Gridlayoutmanager With Custom Divider
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 l
Solution 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:
DrawablehorizontalDivider= ContextCompat.getDrawable(this, R.drawable.line_divider);
DrawableverticalDivider= ContextCompat.getDrawable(this, R.drawable.line_divider);
recyclerView.addItemDecoration(newGridDividerItemDecoration(horizontalDivider, verticalDivider, 4));
My line_divider.xml was as follows:
<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:width="1dp"android:height="1dp" /><solidandroid:color="@android:color/black" /></shape>
This is just a quick answer from me. But this should work, I guess..
Solution 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.LayoutManagermLayoutManager=newGridLayoutManager(getApplicationContext(), 3);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(newDefaultItemAnimator());
DividerItemDecorationHdivider=newDividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
DividerItemDecorationVdivider=newDividerItemDecoration(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"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><sizeandroid:width="1dp"android:height="1dp" /><solidandroid:color="@color/white" /></shape>
Happy Coding!
Solution 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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@android:color/transparent"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@color/black"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@color/black"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@color/black"/></RelativeLayout></LinearLayout>
Post a Comment for "Gridlayoutmanager With Custom Divider"