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 use RecyclerView in Android to realize the effect of suspension suction, separation line and prompt in the end.

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use RecyclerView in Android to achieve suspension ceiling, separation line, in the end prompt effect, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Core implementation point

1. Why can it be realized through ItemDecoration, principle?

① uses the getItemOffsets () method to obtain the left, top, right, and bottom margins of the current template view, which are used to draw these auxiliary ui.

/ / RecyclerView's measure child method public void measureChild (@ NonNull View child, int widthUsed, int heightUsed) {final LayoutParams lp = (LayoutParams) child.getLayoutParams (); / / accumulate the values obtained by getItemOffsets () to the measured values final Rect insets = mRecyclerView.getItemDecorInsetsForChild (child); widthUsed + = insets.left + insets.right; heightUsed + = insets.top + insets.bottom; final int widthSpec = getChildMeasureSpec (getWidth (), getWidthMode (), getPaddingLeft () + getPaddingRight () + widthUsed, lp.width, canScrollHorizontally ()) Final int heightSpec = getChildMeasureSpec (getHeight (), getHeightMode (), getPaddingTop () + getPaddingBottom () + heightUsed, lp.height, canScrollVertically ()); if (shouldMeasureChild (child, widthSpec, heightSpec, lp)) {child.measure (widthSpec, heightSpec);}

② draws a floating view through onDrawOver (), and the ui is drawn on top of all child views.

@ Override public void draw (Canvas c) {super.draw (c); / / call back the onDrawOver () method final int count = mItemDecorations.size () after RecyclerView has been drawn; for (int I = 0; I < count; iDrawover +) {mItemDecorations.get (I) .onDrawover (c, this, mState);}}

③ draws views such as split lines through the onDraw () method.

Public void onDraw (Canvas c) {super.onDraw (c); / / call back the onDraw () method before drawing the RecyclerView sub-view final int count = mItemDecorations.size (); for (int I = 0; I < count; iTunes +) {mItemDecorations.get (I) .onDraw (c, this, mState);}}

2. The drawing of "hint to the end"

Since the width and height of the child view cannot be obtained in getItemOffsets (), there is no measure at this time. After adding the height in getItemOffsets (), if you are not satisfied with a screen, you need to correct it in the onDraw () method. The correction method is: reflection modifies the mDecorInsets property and resets the value set in the getItemOffsets () method.

Private void setDecorInsetsBottom (RecyclerView.LayoutParams param, int bottom) {try {/ / find the mDecorInsets attribute value in RecyclerView.LayoutParams Field filed = RecyclerView.LayoutParams.class.getDeclaredField ("mDecorInsets"); filed.setAccessible (true); Rect decorRect = (Rect) filed.get (param); decorRect.bottom = bottom;} catch (Exception e) {}}

These are all the contents of this article entitled "how to use RecyclerView to achieve suspension ceiling, separation line and hint effect in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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