Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the upper and lower left and right spacer lines in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to customize the upper and lower left and right spacer lines in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this article on how to customize the upper and lower left and right spacer lines in Android, let's take a look.

The code is as follows:

Public class SpaceItemDecoration extends RecyclerView.ItemDecoration {private int space; Paint mPaint; public SpaceItemDecoration (int space) {this.space = space; mPaint = new Paint (Paint.ANTI_ALIAS_FLAG); mPaint.setColor (0xffededed); mPaint.setStyle (Paint.Style.FILL);} private static final int HORIZONTAL = 355; @ Override public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {int itemViewType = parent.getLayoutManager () .getItemViewType (view) If (itemViewType = = HORIZONTAL) {/ / judge whether the line is displayed according to the ViewType type outRect.bottom = space; if (parent.getChildLayoutPosition (view)% 2 = = 0) {outRect.right = space / 2; outRect.left = space;} else {outRect.right = space; outRect.left = space / 2 }} @ Override public void onDraw (Canvas c, RecyclerView parent, RecyclerView.State state) {c.save (); if (parent.getLayoutManager () instanceof GridLayoutManager) {if (GridLayoutManager) parent.getLayoutManager () .getSpanCount () = = 2) {draw (c, parent);}} c.restore () } / / draw horizontal item split line private void draw (Canvas canvas, RecyclerView parent) {int childSize = parent.getChildCount (); for (int I = 0; I < childSize; iTunes +) {View child = parent.getChildAt (I); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams (); int left = child.getLeft (); int right = child.getRight (); int top = child.getBottom () + layoutParams.bottomMargin Int bottom = top + space; if (mPaint! = null) {canvas.drawRect (left, top, right, bottom, mPaint); / / draw the horizontal line under the picture} if (parent.getChildLayoutPosition (child)% 2 = = 0) {canvas.drawRect (0, child.getTop (), left, child.getBottom () + space, mPaint); / / draw the left line} top = child.getTop () Bottom = child.getBottom () + space; left = child.getRight () + layoutParams.rightMargin; right = left + space; if (mPaint! = null) {canvas.drawRect (left, top, right, bottom, mPaint); / / draw the right line of the picture}

How to use:

Private void initSpaceLine () {if (decor = = null) {decor = new SpaceItemDecoration (ViewUtil.dp2px (5)); recycleListView.addItemDecoration (decor);}} this article on "how to customize the upper and lower left and right spacer lines in Android" ends here, thank you for reading! I believe that everyone has a certain understanding of "how to customize the upper and lower left and right spacing lines in Android". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report