您好,登錄后才能下訂單哦!
小編給大家分享一下Android中如何使用RecyclerView實(shí)現(xiàn)懸浮吸頂、分隔線、到底提示效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
核心實(shí)現(xiàn)點(diǎn)
1、為什么通過ItemDecoration能夠?qū)崿F(xiàn),原理?
①通過getItemOffsets()方法獲取當(dāng)前模板view的left、top、right、bottom邊距,這些留出的間距用于繪制這些輔助性u(píng)i。
// RecyclerView的measure child方法public void measureChild(@NonNull View child, int widthUsed, int heightUsed) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); //將getItemOffsets()獲取的值累加到測(cè)量值之中 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); } }
②通過onDrawOver()繪制懸浮視圖,繪制的ui在所有子視圖之上。
@Override public void draw(Canvas c) { super.draw(c); //在RecyclerView繪制完之后回調(diào)onDrawOver()方法 final int count = mItemDecorations.size(); for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDrawOver(c, this, mState); } }
③通過onDraw()方法繪制分割線等視圖。
public void onDraw(Canvas c) { super.onDraw(c); //先回調(diào)onDraw()方法,在繪制RecyclerView子view final int count = mItemDecorations.size(); for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDraw(c, this, mState); } }
2、“到底提示” 的繪制
由于在getItemOffsets()獲取不到子視圖的寬高,此時(shí)還沒有measure,在getItemOffsets()添加高度后,如果不滿一屏需要在onDraw()方法中進(jìn)行修正,修正方式為:反射修改mDecorInsets屬性,重置在getItemOffsets()方法中設(shè)置的值。
private void setDecorInsetsBottom(RecyclerView.LayoutParams param, int bottom) { try { // 找到RecyclerView.LayoutParams中的mDecorInsets屬性值 Field filed = RecyclerView.LayoutParams.class.getDeclaredField("mDecorInsets"); filed.setAccessible(true); Rect decorRect = (Rect) filed.get(param); decorRect.bottom = bottom; } catch (Exception e) { } }
以上是“Android中如何使用RecyclerView實(shí)現(xiàn)懸浮吸頂、分隔線、到底提示效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。