RecyclerView高级应用——自定义ItemDecoration

RecyclerView的用法这里就不讲了,之前加分割线是直接在Item布局加的,后来想想这种解决办法实在是太low了。对技术有追求的人当然要用更高级的办法。啊哈哈哈~

方法讲解

在此之前一定要先介绍一下方法,这样可以更方便你的理解。
我们需要编写一个类继承RecyclerView.ItemDecoration
并重写三种方法:

getItemOffsets:

这个可以简单的理解成为RecyclerView的每一个Item设置一个偏移量你可一个理解成设置了一个margin。

这里写图片描述

上图就是通过getItemOffsets方法中的 outRect.set(0, 0, 0, 100);进行设置的。

如果我们要为RecyclerView增加分割线,我们应该大致做一下的准备:
1.得到系统默认的listDivider属性,并通过它取得对应的Drawable对象。
2.测量Drawable对象的宽高。
3.根据情况判断是需要水平分割线还是垂直分割线

a.水平分割线:
    outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());我们应该底部预留分割线的高度。
b.垂直分割线:
    outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);我们应该右侧预留分割线的宽度。

大家可以看到了,右边空出来些东西,是不是很不美观,那当然就需要往上面画一些东西喽。我们就需要用onDraw()这个方法。

onDraw:

这个方法就是重点了,方法的参数里面有一个Canvas c的对象,了解自定义View的人都应该觉得这个方法很相似我们可以在这上面画文字、bitmap、圆等等等等。

onDrawOver:

这个方法是相对与整个视图的,不受Item的限制,比如我们可以在视图的顶部加一个悬浮的层等等。

这三个方法简单的讲到这里,下面通过实践去更深刻的理解。

代码:

class MyItemDecoration extends RecyclerView.ItemDecoration {


        private final Drawable mLine;
        private final int orientation;

        public MyItemDecoration(Context context, int orientation) {
            this.orientation = orientation;
            int[] attrs = new int[]{android.R.attr.listDivider};
            TypedArray a = context.obtainStyledAttributes(attrs);
            mLine = a.getDrawable(0);
            a.recycle();
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
            if (orientation == RecyclerView.HORIZONTAL) {
                drawVertical(c, parent, state);
            } else if (orientation == RecyclerView.VERTICAL) {
                drawHorizontal(c, parent, state);
            }
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {

            super.onDrawOver(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (orientation == RecyclerView.HORIZONTAL) {
                //画垂直线
                outRect.set(0, 0, mLine.getIntrinsicWidth(), 0);
            } else if (orientation == RecyclerView.VERTICAL) {
                //画水平线
                outRect.set(0, 0, 0, mLine.getIntrinsicHeight());
            }
        }

        /**
         * 画垂直分割线
         */
        private void drawVertical(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                int left = child.getRight();
                int top = c
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要实现 RecyclerView 滚动条高度自定义,你可以考虑使用一个自定义的滚动条 View,然后通过监听 RecyclerView 的滚动事件来控制滚动条的位置和高度。 首先,你需要在 RecyclerView 的布局文件中添加一个自定义的滚动条 View,例如: ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.example.customscrollbar.CustomScrollBar android:id="@+id/custom_scroll_bar" android:layout_width="10dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_marginRight="10dp" /> </RelativeLayout> ``` 其中,CustomScrollBar 是自定义的滚动条 View,它的宽度可以根据需求自行设置。 然后,在 RecyclerView 的代码中,你需要监听 RecyclerView 的滚动事件,计算滚动条的位置和高度,然后更新 CustomScrollBar 的位置和高度。例如: ```java recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 计算当前可见的 item 数量 int visibleItemCount = recyclerView.getLayoutManager().getChildCount(); // 计算所有 item 的数量 int totalItemCount = recyclerView.getLayoutManager().getItemCount(); // 计算第一个可见的 item 的位置 int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); // 计算滚动条的高度 int scrollBarHeight = (int) ((float) visibleItemCount / (float) totalItemCount * recyclerView.getHeight()); // 计算滚动条的位置 int scrollBarTop = (int) ((float) firstVisibleItemPosition / (float) totalItemCount * recyclerView.getHeight()); // 更新滚动条的位置和高度 customScrollBar.setScrollBarTop(scrollBarTop); customScrollBar.setScrollBarHeight(scrollBarHeight); } }); ``` 在这段代码中,我们使用了 RecyclerView 的 LayoutManager 中的方法来计算当前可见的 item 数量、所有 item 的数量和第一个可见的 item 的位置,然后根据这些数据计算出滚动条的位置和高度,最后更新 CustomScrollBar 的位置和高度。 最后,你需要在 CustomScrollBar 中实现 setScrollBarTop() 和 setScrollBarHeight() 方法,用于设置滚动条的位置和高度。例如: ```java public void setScrollBarTop(int top) { setY(top); } public void setScrollBarHeight(int height) { getLayoutParams().height = height; requestLayout(); } ``` 这样,你就可以实现 RecyclerView 滚动条高度自定义的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值