preface
Before writing the article, I specially went to the Internet to check the RecyclerView separation line solution for a long time, roughly divided into two kinds:
First, draw a line in the layout of item. Second, customize item decoration
The first one is probably fine in the LinearLayoutManager, but do you still want to use it in the GridViewLayoutManager? Are you sure you’re not kidding me? The second solution, almost all the examples on the Internet are not very universal, either LinearLayoutManager or GridViewLayoutManager, and there is no flexibility, For example, I want the last item of some LinearLayoutManager not to display the splitter line. None of that worked out.
So I thought, as a man who dreams of becoming one Piece King, I must step up and share my great sword that I have been hiding for so long.
RecyclerView separation line difficult
Nothing more than two points:
- Where to display (which Item? Where is Item?
- What to display (how wide? Dotted line solid line? What color is it? Pure color? Gradient?)
Among them, the most difficult and core problem, which is where to display, has been solved perfectly by this project. What about the second point, according to problems, in view of the most basic is pure color line, there is very little that fancy design, so the project is a temporary style only support custom width and color, but such believe has meet your requirements, but more customizable style will be released soon, welcome to Star, the Fork or PR.
Y_DividerItemDecoration Project address
Y_DividerItemDecoration
A simple introduction
A universal RecyclerView partition line, support LinearLayoutManager and GridViewLayoutManager. In principle, any LayoutManager is supported as long as you can describe the left, right, and top lines of each position
Features
- LinearLayoutManager and GridViewLayoutManager are common
- Customize the width and color of the dividing line
- Flexible control RecyclerView each item left, top, right, bottom sectionline display or not
Usage
Step1
Create a new object that implements the abstract class Y_DividerItemDecoration, In getItemSidesIsHaveOffsets (int itemPosition) defined in the entry when itemPosition clockwise left, top, right, bottom whether need to display line.
class DividerItemDecoration extends Y_DividerItemDecoration {
public DividerItemDecoration(Context context, int lineWidthDp, @ColorInt int colorRGB) {
super(context, lineWidthDp, colorRGB);
}
@Override
public boolean[] getItemSidesIsHaveOffsets(int itemPosition) {
// Clockwise :left, top, right, bottom
boolean[] isOffset = {false.false.false.true};// By default, only bottom displays the dividing line
returnisOffset; }}Copy the code
Step2
Add RecyclerView sectionals and pass in width and color of the sectionals, width is dp, color must be a hex int containing Alpha and R, G, B, e.g. 0xFF6C6C6C
recyclerView.addItemDecoration(new DividerItemDecoration(this.6.0xff6c6c6c));Copy the code
Download
Gradle
compile 'com.yanyusong.y_divideritemdecoration:y_divideritemdecoration:1.0Copy the code
Communication circle
Technical exchange QQ group: 627374460
Or scan code add group directly
appreciates
If Y_DividerItemDecoration saves you a lot of time, could you please buy me a bag of coffee so that I can be more motivated to do it better? Thank you! 😊