diff --git a/app/src/main/java/com/bytedance/demo/MainActivity.kt b/app/src/main/java/com/bytedance/demo/MainActivity.kt index cd03edf..7d69eab 100644 --- a/app/src/main/java/com/bytedance/demo/MainActivity.kt +++ b/app/src/main/java/com/bytedance/demo/MainActivity.kt @@ -52,15 +52,25 @@ class MainActivity : AppCompatActivity(), KrcView.onDraggingListener { } }) - LocatedViewBinding.inflate(layoutInflater, vb.krcView, false).run { - // 点击跳转指定进度。 - btnSeekTo.setOnClickListener { - progress = skipProgress - vb.krcView.setProgress(skipProgress) - vb.seekBar.progress = skipProgress.toInt() +// LocatedViewBinding.inflate(layoutInflater, vb.krcView, false).run { +// // 点击跳转指定进度。 +// btnSeekTo.setOnClickListener { +// progress = skipProgress +// vb.krcView.setProgress(skipProgress) +// vb.seekBar.progress = skipProgress.toInt() +// } +// // 设置 located view +// vb.krcView.locatedView = this.root +// } + vb.krcView.locatedView?.let { + LocatedViewBinding.bind(it).run { + // 点击跳转指定进度。 + btnSeekTo.setOnClickListener { + progress = skipProgress + vb.krcView.setProgress(skipProgress) + vb.seekBar.progress = skipProgress.toInt() + } } - // 设置 located view - vb.krcView.locatedView = this.root } // 设置拖拽歌词监听器 vb.krcView.setOnDraggingListener(this) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 04938c7..1a43989 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -20,6 +20,7 @@ app:max_text_size="18sp" app:min_text_size="15sp" app:maxWordsPerLine="15" + app:located_view_layout="@layout/located_view" app:current_line_top_offset="70dp" android:layout_marginBottom="10dp" /> diff --git a/krcview/src/main/java/com/bytedance/krcview/KrcView.java b/krcview/src/main/java/com/bytedance/krcview/KrcView.java index 62d7d24..af9381e 100644 --- a/krcview/src/main/java/com/bytedance/krcview/KrcView.java +++ b/krcview/src/main/java/com/bytedance/krcview/KrcView.java @@ -25,6 +25,7 @@ import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; +import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -34,6 +35,7 @@ import androidx.annotation.IntRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.view.LayoutInflaterCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; @@ -127,7 +129,7 @@ public KrcView(@NonNull Context context, @Nullable AttributeSet attrs) { public KrcView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - recyclerView = new RecyclerView(context){ + recyclerView = new RecyclerView(context) { @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent e) { @@ -205,6 +207,11 @@ protected int getExtraLayoutSpace(State state) { currentLineTextColor = readAttrColor(a, R.styleable.KrcView_current_line_text_color, normalTextColor); currentLineHLTextColor = readAttrColor(a, R.styleable.KrcView_current_line_highLight_text_color, Color.GREEN); isUserInputEnabled = a.getBoolean(R.styleable.KrcView_isUserInputEnabled, true); + final int locatedViewLayoutId = a.getResourceId(R.styleable.KrcView_located_view_layout, View.NO_ID); + if (locatedViewLayoutId != View.NO_ID) { + locatedView = LayoutInflater.from(getContext()).inflate(locatedViewLayoutId, this, false); + setLocatedView(locatedView); + } a.recycle(); if (lineSpace > 0f) { recyclerView.addItemDecoration(new ItemDecoration() { @@ -365,6 +372,7 @@ public void setLocatedView(View view) { locatedView = view; view.setVisibility(View.INVISIBLE); addView(view); + post(this::updateLocateViewTopOffset); } @@ -389,6 +397,7 @@ public void setKrcData(List data) { } } recyclerView.setAdapter(new AdapterImpl()); + post(this::updateLocateViewTopOffset); } @@ -544,8 +553,7 @@ void setLineProgress(final long lineProgress) { return; } this.lineProgress = lineProgress; -// KrcLineView.this.invalidate(); - invalidate(); + KrcLineView.this.invalidate(); } @@ -792,27 +800,13 @@ public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newStat private void tryToShowLocatedView() { removeCallbacks(hideLocatedViewTask); - if (locatedView == null || curLineIndex < 0 || locatedView.getVisibility() == View.VISIBLE) { + if (locatedView == null || locatedView.getVisibility() == View.VISIBLE) { return; } locatedView.setVisibility(View.VISIBLE); } - private void updateLocateViewTopOffset() { - if (locatedView == null || curLineIndex < 0 || locatedView.getVisibility() == View.VISIBLE) { - return; - } - final ViewHolder curVH = recyclerView.findViewHolderForAdapterPosition(curLineIndex); - if (curVH == null) { - return; - } - final int newLocateViewTopOffset = curVH.itemView.getBottom() - (locatedView.getHeight() >> 1); - if (newLocateViewTopOffset != locateViewTopOffset) { - locateViewTopOffset = newLocateViewTopOffset; - logI("===>updateLocateViewTopOffset: " + locateViewTopOffset); - requestLayout(); - } - } + private void tryToHideLocatedViewDelay() { if (locatedView == null || locatedView.getVisibility() != VISIBLE) { @@ -822,10 +816,10 @@ private void tryToHideLocatedViewDelay() { } private void notifyStartDragging() { - if (onDraggingListener == null || curLineIndex < 0) { + if (onDraggingListener == null) { return; } - final ViewHolder cur = recyclerView.findViewHolderForAdapterPosition(curLineIndex); + final ViewHolder cur = recyclerView.findViewHolderForAdapterPosition(curLineIndex == -1 ? 0 : curLineIndex); if (cur != null) { locatedItemView = (KrcLineView) cur.itemView; onDraggingListener.onStartDragging(KrcView.this, locatedItemView.krcLineInfo, @@ -873,6 +867,22 @@ private void notifyDragging() { } }; + private void updateLocateViewTopOffset() { + if (locatedView == null || locatedView.getVisibility() == View.VISIBLE) { + return; + } + final ViewHolder curVH = recyclerView.findViewHolderForAdapterPosition( + curLineIndex == -1 ? 0 : curLineIndex); + if (curVH == null) { + return; + } + final int newLocateViewTopOffset = curVH.itemView.getBottom() - (locatedView.getHeight() >> 1); + if (newLocateViewTopOffset != locateViewTopOffset) { + locateViewTopOffset = newLocateViewTopOffset; + logI("===>updateLocateViewTopOffset: " + locateViewTopOffset); + requestLayout(); + } + } public interface onDraggingListener { /** diff --git a/krcview/src/main/res/values/attrs.xml b/krcview/src/main/res/values/attrs.xml index ad52f10..09ce5b5 100644 --- a/krcview/src/main/res/values/attrs.xml +++ b/krcview/src/main/res/values/attrs.xml @@ -10,6 +10,7 @@ + \ No newline at end of file