Recyclerview顶部水平滑动条(Seekbar)
水平滑动 效果图
<SeekBar
android:id="@+id/horizontal_slide"
android:layout_width="480dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:focusable="true"
android:maxHeight="10dp"
android:minHeight="10dp"
android:visibility="gone"
android:progressDrawable="@drawable/bg_fast_scroll_bar_track"
android:thumb="@drawable/bg_fast_scroll_bar_thumb"
android:layout_marginTop="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/rv_music"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<com.flyaudio.flyaudiohomeapp.view.CustomRecyclerView
android:id="@+id/rv_music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
bg_fast_scroll_bar_track.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="#CCCCCC" />
<size
android:width="480dp"
android:height="30dp" />
</shape>
bg_fast_scroll_bar_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="6dp"/>
<solid android:color="#ff0000" />
<size
android:width="100dp"
android:height="10dp" />
</shape>
private RecyclerView recyclerView;
private SeekBar horizontalSlide;
private int extent,range,offset;
//按recyclerview的具体item的位置滑动
private boolean isScroll=false;
//false:recyclerview的x距离滑动 true:按recyclerview的具体item的位置滑动
private boolean isItemPositionScroll=false;
GridViewAdapter gridViewAdapter=new GridViewAdapter((List<App>) appList,R.layout.item_top_music,0);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(gridViewAdapter);
extent = recyclerView.computeHorizontalScrollExtent();
//整体的高度,注意是整体,包括在显示区域之外的。
range = recyclerView.computeHorizontalScrollRange();
//已经向下滚动的距离,为0时表示已处于顶部。
offset = recyclerView.computeHorizontalScrollOffset();
horizontalSlide.setPadding(0, 0, 0, 0);
horizontalSlide.setThumbOffset(0);
GradientDrawable gradientDrawable =(GradientDrawable) horizontalSlide.getThumb();
gradientDrawable.setSize(1440/(appList.size()/2),10);
final GradientDrawable mGroupDrawable = (GradientDrawable) horizontalSlide.getThumb();
mGroupDrawable.setSize(1000,10);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isScroll){
return;
}
//显示区域的高度。
extent = recyclerView.computeHorizontalScrollExtent();
//整体的高度,注意是整体,包括在显示区域之外的。
range = recyclerView.computeHorizontalScrollRange();
//已经向下滚动的距离,为0时表示已处于顶部。
offset = recyclerView.computeHorizontalScrollOffset();
Log.i("dx------",range+"****"+extent+"****"+offset);
//此处获取seekbar的getThumb,就是可以滑动的小的滚动游标
GradientDrawable gradientDrawable =(GradientDrawable) horizontalSlide.getThumb();
//根据列表的个数,动态设置游标的大小,设置游标的时候,progress进度的颜色设置为和seekbar的颜色设置的一样的,所以就不显示进度了。
gradientDrawable.setSize(extent/(appList.size()/2),10);
//设置可滚动区域
horizontalSlide.setMax((int)(range-extent));
if (dx==0){
horizontalSlide.setProgress(0);
}else if (dx>0){
// int ss = (int)(tt/2.3f);
Log.i("dx------","右滑");
horizontalSlide.setProgress(offset);
}else if (dx<0){
Log.i("dx------","左滑");
horizontalSlide.setProgress(offset);
}
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState==RecyclerView.SCROLL_STATE_IDLE){//recyclerview停止滑动
isScroll=false;
}
}
});
horizontalSlide.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
isScroll=fromUser;
if(!isItemPositionScroll){
//recyclerview滑动的距离 seekbar进度-recyclerview已经滑动的距离
int dx=progress-recyclerView.computeHorizontalScrollOffset();
recyclerView.smoothScrollBy(dx,0);
}else{
if (fromUser){
//滑动到Recyclerview的具体item
recyclerView.smoothScrollToPosition((int) (progress*1.0f/(range-extent)* Objects.requireNonNull(recyclerView.getAdapter()).getItemCount()));
Log.d("TAG", "onProgressChanged: seekbar distance of slide : "+progress*1.0f/(range-extent)*recyclerView.getAdapter().getItemCount());
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
仿:
https://blog.csdn.net/u010731746/article/details/83303190