国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Android中如何使用RecyclerView實(shí)現懸浮吸頂、分隔線(xiàn)

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

小編給大家分享一下Android中如何使用RecyclerView實(shí)現懸浮吸頂、分隔線(xiàn)、到底提示效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

核心實(shí)現點(diǎn)

1、為什么通過(guò)ItemDecoration能夠實(shí)現,原理?

①通過(guò)getItemOffsets()方法獲取當前模板view的left、top、right、bottom邊距,這些留出的間距用于繪制這些輔助性ui。

// RecyclerView的measure child方法public void measureChild(@NonNull View child, int widthUsed, int heightUsed) {      final LayoutParams lp = (LayoutParams) child.getLayoutParams();  //將getItemOffsets()獲取的值累加到測量值之中      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);      }    }

②通過(guò)onDrawOver()繪制懸浮視圖,繪制的ui在所有子視圖之上。

@Override  public void draw(Canvas c) {    super.draw(c); //在RecyclerView繪制完之后回調onDrawOver()方法    final int count = mItemDecorations.size();    for (int i = 0; i < count; i++) {      mItemDecorations.get(i).onDrawOver(c, this, mState);    } }

③通過(guò)onDraw()方法繪制分割線(xiàn)等視圖。

public void onDraw(Canvas c) {    super.onDraw(c); //先回調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í)還沒(méi)有measure,在getItemOffsets()添加高度后,如果不滿(mǎn)一屏需要在onDraw()方法中進(jìn)行修正,修正方式為:反射修改mDecorInsets屬性,重置在getItemOffsets()方法中設置的值。

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) {    }  }

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

人妻少妇精品专区性色AV| 特级AAAAAAAAA毛片免费视频| 欧美激情一区二区久久久| 人妻丰满熟妇AV无码区不卡| 美女视频黄频A免费高清不卡| 国内揄拍高清国内精品对白 |