Android: RecyclerView is the simplest and most elegant way to add a dividing line ItemDecoration

    First conclusion:

    Everyone knows that RecyclerView does not have a dividing line by default. We can add a dividing line to RecyclerView in various ways, and even add a thin line directly to the layout file. However, a large number of similar uses is somewhat inconvenient and makes The code seems a bit bloated.

    If you just need to simply add a dividing line to a most commonly used vertical list RecyclerView, then don't be too troublesome, just use the following line of code to add a dividing line while setting the layout manager (setLayoutManager) for RecyclerView:

recyclerview.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    At this time, RecyclerView will add the system default gray dividing line:

RecyclerView default gray dividing line
The default gray dividing line of RecyclerView

    If you need to modify the color of the default dividing line, or some Android versions add the line of code above and the dividing line does not appear, then add the listDivider attribute to the Theme (styles.xml file) used by the Activity and specify a color at this time To:

<item name="android:listDivider">@android:color/black</item>

The effect is as follows:

RecyclerView sets the color of the dividing line
RecyclerView sets the color of the dividing line

    Usually the Theme used by the Activity of a project is uniform. After setting here, every time RecyclerView needs to add a dividing line, only one line of code can be done. If you have higher requirements for the dividing line, listen to the breakdown below.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1. Custom dividing line

private class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        private int mSpace;

        public SpaceItemDecoration() {
            float density = getResources().getDisplayMetrics().density;
            this.mSpace = (int) (20 * density + 0.5f);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            if (parent.getChildAdapterPosition(view) % spanCount == 0) {
                outRect.left = mSpace;
            }
        }
    }

2、

        rvList.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(new ColorDrawable(Color.BLACK));
        //RecyclerView添加分割线
        rvList.addItemDecoration(divider);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(getResources().getDrawable(R.drawable.shape_recycler_divider));
        //RecyclerView添加分割线
        rvList.addItemDecoration(divider);

3、

rvList.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    <style name="DesignCompatActivity" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:listDivider">@drawable/shape_recycler_divider</item>
    </style>

 

Guess you like

Origin blog.csdn.net/beita08/article/details/105294353