记录跟随recyclerview滑动的指示器

老早之前做的一个功能,横向recyclerview滑动时,底部做跟随滑动指示器。今天代码不用了,记录下代码。

XML 复制代码
<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/fourteen"
                    android:orientation="vertical"
                    android:paddingLeft="@dimen/sixteen"
                    android:paddingRight="@dimen/sixteen">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rv_teacher"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                    </androidx.recyclerview.widget.RecyclerView>

                    <RelativeLayout
                        android:id="@+id/scroll_bar_bg"
                        android:layout_width="30dp"
                        android:layout_height="4dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="@dimen/six"
                        android:background="@drawable/bg_fast_view">

                        <View
                            android:id="@+id/scroll_bar"
                            android:layout_width="20dp"
                            android:layout_height="4dp"
                            android:layout_centerVertical="true"
                            android:background="@drawable/bg_scroll_bar" />
                    </RelativeLayout>

                </LinearLayout>

bg_fast_view

XML 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid
        android:color="@color/Gray"/>

    <corners
        android:radius="@dimen/ten"/>

</shape>

bg_scroll_bar

XML 复制代码
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid
        android:color="@color/title_bg"/>

    <corners
        android:radius="@dimen/ten"/>

</shape>
java 复制代码
RelativeLayout scrollBarBg = view.findViewById(R.id.parent_layout);
View scrollBar = view.findViewById(R.id.main_line);

 GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvTeacher.setLayoutManager(layoutManager);
            teacherAdapter = new RecommendTeacherRVAdapter(mContext);
            rvTeacher.setAdapter(teacherAdapter);
            rvTeacher.addOnScrollListener(new RecyclerView.OnScrollListener() {
                public int range = 0;

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    //整体的总宽度,注意是整体,包括在显示区域之外的。
                    int temp = recyclerView.computeHorizontalScrollRange();
                    if (temp > range) {
                        range = temp;
                    }
                    int offset = recyclerView.computeHorizontalScrollOffset();
                    int extent = recyclerView.computeHorizontalScrollExtent();

                    float proportion = (float) (offset * 1.0 / (range - extent));
                    //计算滚动条宽度
                    float transMaxRange = scrollBarBg.getWidth() - scrollBar.getWidth();
                    //设置滚动条移动
                    scrollBar.setTranslationX(transMaxRange * proportion);
                }

            });
相关推荐
镰刀出海8 个月前
Recyclerview缓存原理
java·开发语言·缓存·recyclerview·android面试
OH五星上将1 年前
OpenHarmony UI动画-recyclerview_animators
ui·移动开发·动画·openharmony·recyclerview·arkui·鸿蒙开发
骐骥11 年前
2024-08-16升级记录:使用Android RecyclerView控件显示列表型信息
android·recyclerview·列表
图王大胜1 年前
Android APP 基于RecyclerView框架工程(知识体系积累)
android·项目管理·管理·recyclerview·引用管理
小曾同学.com1 年前
Android中的适配器,你知道是做什么的吗?
android·recyclerview·listview·android适配器·android adapter·kotlin语言·arrayadapter适配器
吕氏春秋i1 年前
android recyclerview 刷新 数据错乱的问题
android·缓存·组件·recyclerview·mmkv
yueqc11 年前
RecyclerView 与 ListView(二):缓存
缓存·recyclerview·listview
Modu_MrLiu1 年前
Android基础进阶 - RecyclerView列表加载多类型视图
android·recyclerview·列表加载多类型视图
Just_Paranoid1 年前
掌握 Android 中的 RecyclerView 优化
android·recyclerview