GridLayoutManager with custom divider

一世执手 提交于 2019-12-04 12:19:14

问题


I am trying to add custom divider in RecyclerView with GridLayoutManager but not getting success, i have searched a lot and looked into below mention answer but it didn't help me

link 1
link 2
link 3

I want to put black line in between each items of RecyclerView, something like below.

I have got horizontal line in between each row, but not able to find how to get those lines in between columns also.

chintan soni's answer worked perfectly, but it is creating problem in one scenario only, when i am having 5 views, it shows divider of other 3 items also, like below :


回答1:


Check this out: https://bignerdranch.github.io/simple-item-decoration/

Add this to your app level gradle and sync:

compile 'com.bignerdranch.android:simple-item-decoration:1.0.0'

Then, apply code as below:

    Drawable horizontalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider);
    Drawable verticalDivider = ContextCompat.getDrawable(this, R.drawable.line_divider);
    recyclerView.addItemDecoration(new GridDividerItemDecoration(horizontalDivider, verticalDivider, 4));

My line_divider.xml was as follows:

<?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="@android:color/black" />

</shape>

This is just a quick answer from me. But this should work, I guess..

Output:




回答2:


Simply, write your XML file in layout with a RecyclerView In your Activity write the following code to achieve divider for GridLayoutManager in RecyclerView

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
DividerItemDecoration Hdivider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
DividerItemDecoration Vdivider = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
Hdivider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.divider));
Vdivider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.divider));
recyclerView.addItemDecoration(Hdivider);
recyclerView.addItemDecoration(Vdivider);

Above, both Horizontal and Vertical divider are added to get the whole grid look. The Drawable file can look exactly as you like for your application. Mine looks like this.

   <?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/white" />
    </shape>

Happy Coding!




回答3:


As per you have four columns I create as per that. This will have three straight vertical line for four columns

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/black"/>
    </RelativeLayout>
</LinearLayout>


来源:https://stackoverflow.com/questions/39490124/gridlayoutmanager-with-custom-divider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!