Android recycleview split line colored egg

Time:2021-8-28

I have collected some learning materials, including a lot of learning, interview, middle and high-level fluuter materials, and a lot of video explanations. If any students want to know more, please see the end of the article for details. We also welcome all the great gods to install X.

First of all, the problem

How do you do it?

When we have free time, we need to haveHaving an affair

realization

First of all, the problem

Android recycleview split line colored egg

This is a vertical list made of recycleview. At the bottom of the list, it is close to the bottom of the screen, but we need to leave some space for processing. It seems normal.

How do you do it?

For example, a 50dp view is reserved at the bottom of the last item. When binding data, the adapter judges whether to display the transparent 50dp view according to position. This operation is OK, and I have done it before. It doesn’t seem very elegant. (whatever, let’s make the effect first)

When we have free time, we need to haveHaving an affair

Today, I inadvertently thought that such a bottom view also belongs to an item. It can be understood this way. I also think of Master Kang’s itemdecoration tutorial. The specific video address can’t be found (please forgive me), that is, we can draw a division line at the end of the last item, but it is a transparent division line, so that we can top up the content at the bottom. 1) If you want to use margin and padding to deal with this effect, it is not beautiful. An area will be left at the bottom without displaying the content. 2) There seems to be no other way.

realization

We can understand that all the previous dividing lines are not drawn, but the last one is drawn

@Override public void getitemoffsets (rect outrect, view view, recyclerview parent, recyclerview. State) {/ / my drawable object if (mdivider = = null) {/ / the offset is 0 outrect.set (0, 0, 0, 0); return; } If (morientation = = vertical) {/ / process the vertical direction and obtain the position int lastposition = state. Getitemcount() – 1 of the last item// Position int position of the current item = parent.getchildadapterposition (view)// If (position > = lastposition) {/ / set the offset to the drawable height outrect. Set (0, 0, 0, mdivider. Getinternalheight());} Else {/ / except for the height of the last split line, the height of other split lines is 0. Outrect.set (0, 0, 0, 0);}} Else {/ / the horizontal direction can be ignored. Similarly,}}

The above is the last offset. Let’s take a look at the draw method

@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if (parent.getLayoutManager() == null || mDivider == null) { return; } if (mOrientation == VERTICAL) { drawVertical(c, parent, state); } Else {/ / we don’t need the horizontal direction. We can not write this. Of course, we can also write}}

private void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State state) { canvas.save(); final int left; final int right; if (parent.getClipToPadding()) { left = parent.getPaddingLeft(); right = parent.getWidth() – parent.getPaddingRight(); canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() – parent.getPaddingBottom()); } else { left = 0; right = parent.getWidth(); }

final int childCount = parent.getChildCount(); final int lastPosition = state.getItemCount() – 1; for (int i = 0; i= lastPosition) { parent.getDecoratedBoundsWithMargins(child, mBounds); final int bottom = mBounds.bottom + Math.round(child.getTranslationY()); final int top = bottom – mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(canvas); } } canvas.restore(); }

Drawable settings and favorite colors, I need transparency here, that is #00000000. Above

https://shimo.im/docs/dYkqrQcyr98jPKYX/ 《 Android learning interview fuluter advanced materials are available for free. You can copy the link and open it with graphite document app or applet.

Recommended Today

Objective-C basic data type

Objective-C basic data type Because Objective-C (hereinafter referred to as objc) is essentially a superset of C language, objc also supports all the basic data types supported by C language, and objc also supports some other common data types. Int and nsinteger Int in C language is also supported in objc, but it is not […]