Android Recyclerview grid layout split line,

Source: Internet
Author: User

Android Recyclerview grid layout split line,

1 When I used the imitation image selector in the previous article, the split line came out with a bug. In fact, I deleted a key piece of code, but it didn't matter. This time I wrote a separate article.

The master hongyang on 2csdn has already written this article, but according to his writing method, I tried the grid layout problem myself, I can't remember what the problem is. (I didn't question the meaning of the code. It seems that a method of his code has expired .)

Step 3: Create a New DiciderGridItemDecoration

Import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. rect; import android. graphics. drawable. drawable; import android. support. annotation. intDef; import android. support. v4.view. viewCompat; import android. support. v7.widget. gridLayoutManager; import android. support. v7.widget. recyclerView; import android. support. v7.widget. staggeredGridLayo UtManager; import android. util. sparseArray; import android. util. sparseIntArray; import android. view. view; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy;/*** Created by MAC on 2018/2/5. */public class DividerGridItemDecoration extends RecyclerView. itemDecoration {private static final int [] ATTRS = new int [] {android. r. attr. listDivider}; // This is the place where the style is set (color, width, etc.) public Static final int GRID_DIVIDER_HORIZONTAL = GridLayoutManager. HORIZONTAL; public static final int GRID_DIVIDER_VERTICAL = GridLayoutManager. VERTICAL; private final SparseIntArray inline = new SparseIntArray (); private final SparseArray <DrawableCreator> inline = new SparseArray <> (); @ IntD Ef ({GRID_DIVIDER_HORIZONTAL, GRID_DIVIDER_VERTICAL}) @ Retention (RetentionPolicy. SOURCE) private @ interface Orientation {}@ Orientation private int mOrientation; private Drawable vertex; private Drawable mVerticalDivider; public vertex (Context context, @ Orientation int orientation) {resolveDivider (context ); setOrientation (orientation);} private void resolveDivide R (Context context) {final TypedArray a = context. obtainStyledAttributes (ATTRS); mVerticalDivider = mHorizontalDivider =. getDrawable (0);. recycle ();} public void setOrientation (int orientation) {this. mOrientation = orientation;} public void setVerticalDivider (Drawable verticalDivider) {this. mVerticalDivider = verticalDivider;} public void setHorizontalDivider (Drawable horizontalDivider ){ This. mHorizontalDivider = horizontalDivider;} @ Override public void onDraw (Canvas c, RecyclerView parent, RecyclerView. state state) {Centers (c, parent); drawVerticalDividers (c, parent) ;}@ Override public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView. state state) {final int spanCount = getSpanCount (parent); final int childCount = parent. getAdapter (). GetItemCount (); final int adapterPosition = parent. getChildAdapterPosition (view); if (mHorizontalDividerOffsets. indexOfKey (adapterPosition) <0) {mHorizontalDividerOffsets. put (adapterPosition, getHorizontalDivider (parent, adapterPosition ). getIntrinsicHeight ();} if (mVerticalDividerOffsets. indexOfKey (adapterPosition) <0) {mVerticalDividerOffsets. put (adapterPosition, getVerticalDivider (paren T, adapterPosition ). getIntrinsicHeight ();} outRect. set (0, 0, mHorizontalDividerOffsets. get (adapterPosition), mVerticalDividerOffsets. get (adapterPosition); if (isLastRow (adapterPosition, spanCount, childCount) {outRect. bottom = 0;} if (isLastColumn (adapterPosition, spanCount, childCount) {outRect. right = 0 ;}} private boolean isLastColumn (int position, int spanCount, int childCount) {if (MOrientation = GRID_DIVIDER_VERTICAL) {return (position + 1) % spanCount = 0;} else {int lastColumnCount = childCount % spanCount; lastColumnCount = 0? SpanCount: lastColumnCount; return position> = childCount-lastColumnCount;} private boolean isLastRow (int position, int spanCount, int childCount) {if (mOrientation = GRID_DIVIDER_VERTICAL) {int lastColumnCount = childCount % spanCount; lastColumnCount = 0? SpanCount: lastColumnCount; return position> = childCount-lastColumnCount;} else {return (position + 1) % spanCount = 0 ;}} private int getSpanCount (RecyclerView parent) {RecyclerView. layoutManager layoutManager = parent. getLayoutManager (); if (layoutManager instanceof GridLayoutManager) {return (GridLayoutManager) layoutManager ). getSpanCount ();} else if (layoutManager instanceof Stagge RedGridLayoutManager) {return (StaggeredGridLayoutManager) layoutManager ). getSpanCount ();} else {throw new UnsupportedOperationException ("the role can only be used in" + "the RecyclerView which use a GridLayoutManager or StaggeredGridLayoutManager ");}} public void drawVerticalDividers (Canvas c, RecyclerView parent) {final int left = parent. getPaddingLeft (); final int r Ight = parent. getWidth ()-parent. getPaddingRight (); final int childCount = parent. getChildCount (); for (int I = 0; I <childCount; I ++) {final View child = parent. getChildAt (I); final RecyclerView. layoutParams params = (RecyclerView. layoutParams) child. getLayoutParams (); final Drawable divider = getVerticalDivider (parent, params. getViewAdapterPosition (); final int top = child. getBottom () + para Ms. bottomMargin + Math. round (ViewCompat. getTranslationY (child); final int bottom = top + divider. getIntrinsicHeight (); mVerticalDividerOffsets. put (params. getViewAdapterPosition (), divider. getIntrinsicHeight (); divider. setBounds (left, top, right, bottom); divider. draw (c) ;}} public void drawHorizontalDividers (Canvas c, RecyclerView parent) {final int top = parent. getPaddingTop (); final int bott Om = parent. getHeight ()-parent. getPaddingBottom (); final int childCount = parent. getChildCount (); for (int I = 0; I <childCount; I ++) {final View child = parent. getChildAt (I); final RecyclerView. layoutParams params = (RecyclerView. layoutParams) child. getLayoutParams (); final Drawable divider = getHorizontalDivider (parent, params. getViewAdapterPosition (); final int left = child. getRight () + pa Rams. rightMargin + Math. round (ViewCompat. getTranslationX (child); final int right = left + divider. getIntrinsicHeight (); mHorizontalDividerOffsets. put (params. getViewAdapterPosition (), divider. getIntrinsicHeight (); divider. setBounds (left, top, right, bottom); divider. draw (c) ;}} private Drawable getVerticalDivider (RecyclerView parent, int adapterPosition) {RecyclerView. adapter adapter = parent. g EtAdapter (); int itemType = adapter. getItemViewType (adapterPosition); DrawableCreator drawableCreator = mTypeDrawableFactories. get (itemType); if (drawableCreator! = Null) {return drawableCreator. createVertical (parent, adapterPosition);} return mVerticalDivider;} private Drawable getHorizontalDivider (RecyclerView parent, int adapterPosition) {RecyclerView. adapter adapter = parent. getAdapter (); int itemType = adapter. getItemViewType (adapterPosition); DrawableCreator drawableCreator = mTypeDrawableFactories. get (itemType); if (drawableCreator! = Null) {return drawableCreator. createHorizontal (parent, adapterPosition);} return mHorizontalDivider;} public void registerTypeDrawable (int itemType, DrawableCreator drawableCreator) {mTypeDrawableFactories. put (itemType, drawableCreator);} public interface DrawableCreator {Drawable createVertical (RecyclerView parent, int adapterPosition); Drawable createHorizontal (RecyclerView parent, int adapterPosition );}}

4. Next, create a split line style in the drawable folder, divider_bg.xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android" android: shape = "rectangle"> <gradient // said, This is set to gradient, in order to save trouble, I directly change the three colors to the same color (normally writing corners, solid, and stroke should also be acceptable. I did not try it myself) android: centerColor = "# F5F6F6" android: endColor = "# F5F6F6" android: startColor = "# F5F6F6" android: type = "linear"/> <size android: height = "4dp"/> </shape>

5. Next, find them in the application of manifests and click to enter and modify

<Style name = "PlayerTheme" parent = "Theme. appCompat. noActionBar "> <item name =" android: windowAnimationStyle "> @ style/PlayerAnimation </item> <item name =" android: listDivider "> @ drawable/divider_bg </item> // Add this sentence. Other code has nothing to do with the split line (For details, refer to the hongyang blog) </style>

6. Use

Binding. recyclerview. setLayoutManager (new GridLayoutManager (context, 2); binding. recyclerview. addItemDecoration (new DividerGridItemDecoration (context, DividerGridItemDecoration. GRID_DIVIDER_VERTICAL); // The final parameter should be set to the direction of the split line. If the split line is not discolored, it doesn't matter how to set it. You can decide the direction of the colored line.

7. To sum up, it is actually very simple. Copy DividerGridItemDecoration, write a style, add the style to them, and then call a line of code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.