zoukankan      html  css  js  c++  java
  • 解决RecyclerView中没有Divider的问题

    public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
        private Drawable mDivider;
     
        public SimpleDividerItemDecoration(Context context) {
            mDivider = context.getResources().getDrawable(R.drawable.line_divider);
        }
     
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
     
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
     
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
     
                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
     
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
     
        <size
            android:width="1dp"
            android:height="1dp" />
     
        <solid android:color="@color/dark_gray" />
     
    </shape>

    Useage:

        mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(
                getApplicationContext()
            ));
  • 相关阅读:
    vue bus 中央事件总线
    0时间复杂度
    stack 数据结构
    es6 class
    directives 自定义指令
    node中间件
    数据结构博客清单
    TCP/IP 协议栈博客清单
    Java 面向对象:接口
    Java 面向对象:Object 类
  • 原文地址:https://www.cnblogs.com/wkun/p/4374570.html
Copyright © 2011-2022 走看看