溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

發(fā)布時(shí)間:2020-10-01 00:10:47 來(lái)源:腳本之家 閱讀:421 作者:帶心情去旅行 欄目:移動(dòng)開(kāi)發(fā)

本文介紹了Android RecyclerView懸浮效果,分享給大家,具體如下:

先看個(gè)效果

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

這是一個(gè)City列表,每個(gè)City都有所屬的Province,需要在滑動(dòng)的時(shí)候,將對(duì)應(yīng)的Province懸浮在頂部。懸浮頂部的Province需要根據(jù)列表的滑動(dòng)而適當(dāng)改變位置,實(shí)現(xiàn)“頂上去”的效果。

實(shí)現(xiàn)思路:

  1. 利用RecyclerView.ItemDecoration繪制Province(就像繪制分割線(xiàn)一樣)
  2. 同一組的City,只繪制一個(gè)Province
  3. 計(jì)算偏移,將當(dāng)前Province固定在頂部
  4. 根據(jù)列表滑動(dòng),實(shí)現(xiàn)偏移效果

ItemDecoration

既然是利用RecyclerView.ItemDecoration實(shí)現(xiàn)的懸浮效果,那么有必要了解下它。

ItemDecoration字面意思:Item的裝飾。是的!是裝飾!不只是畫(huà)分割線(xiàn)。

其實(shí)ItemDecoration的功能非常強(qiáng)大,而我們平時(shí)只是用它來(lái)實(shí)現(xiàn)分割線(xiàn)的效果(至少我是這樣)。因此,可能很多同學(xué)認(rèn)為ItemDecoration就是用來(lái)繪制分割線(xiàn)的。其實(shí)不然,ItemDecoration的功能遠(yuǎn)不止是分割線(xiàn)的繪制。

先看下RecyclerView.ItemDecoration的源碼(部分):

public static abstract class ItemDecoration {
 ...
 public void onDraw(Canvas c, RecyclerView parent, State state) {
  onDraw(c, parent);
 }
 public void onDrawOver(Canvas c, RecyclerView parent, State state) {
  onDrawOver(c, parent);
 }
 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
  getItemOffsets(outRect, ((LayoutParams) view.getLayoutParams()).getViewLayoutPosition(),
    parent);
 }
}

里面是我們常用的三個(gè)方法:

  1. getItemOffsets:通過(guò)Rect為每個(gè)Item設(shè)置偏移,用于繪制Decoration。
  2. onDraw:通過(guò)該方法,在Canvas上繪制內(nèi)容,在繪制Item之前調(diào)用。(如果沒(méi)有通過(guò)getItemOffsets設(shè)置偏移的話(huà),Item的內(nèi)容會(huì)將其覆蓋)
  3. onDrawOver:通過(guò)該方法,在Canvas上繪制內(nèi)容,在Item之后調(diào)用。(畫(huà)的內(nèi)容會(huì)覆蓋在item的上層)

RecyclerView 的背景、onDraw繪制的內(nèi)容、Item、onDrawOver繪制的內(nèi)容,各層級(jí)關(guān)系如下:

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

層級(jí)關(guān)系

繪制分割線(xiàn)

先看看一般的分割線(xiàn)繪制。

定義分割線(xiàn)高度

private int mHeight = 5; //分割線(xiàn)高度

通過(guò)getItemOffsets預(yù)留空間

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
 super.getItemOffsets(outRect, view, parent, state);
 int position = parent.getChildAdapterPosition(view);
 if (position != 0) {
  //第一個(gè)item預(yù)留空間
  outRect.top = mHeight;
 }
}

然后在onDraw中繪制

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
 super.onDraw(c, parent, state);
 final int left = parent.getLeft();
 final int right = parent.getRight();
 final int childCount = parent.getChildCount();
 for (int i = 0; i < childCount; i++) {
  final View childView = parent.getChildAt(i);
  final int bottom = childView.getTop();
  final int top = bottom - mHeight;
  c.drawRect(left, top, right, bottom, mPaint);
 }
}

獲取當(dāng)前RecyclerView的Item數(shù)量,遍歷每個(gè)Item。在對(duì)應(yīng)的位置繪制一個(gè)高度為mHeight的矩形 ,從而實(shí)現(xiàn)分割線(xiàn)的效果。

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

(詳情代碼見(jiàn)底部鏈接)

打造懸浮效果

這是一個(gè)城市列表,根據(jù)省份分組,相同的城市只會(huì)顯示一個(gè)省份。滾動(dòng)城市列表時(shí),省份會(huì)懸浮在頂部。效果如下:

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

實(shí)現(xiàn)

由于需要懸浮效果,所以需要在onDrawOver中繪制分組。

定義一個(gè)interface,根據(jù)position通過(guò)接口方法getGroupName獲取當(dāng)前省名(由Activity實(shí)現(xiàn))

public interface GroupListener {
 String getGroupName(int position);
}

創(chuàng)建StickyDecoration繼承RecyclerView.ItemDecoration

定義isFirstInGroup方法。根據(jù)前一個(gè)省份,判斷當(dāng)前是否為新的省份

//判斷是不是組中的第一個(gè)位置
//根據(jù)前一個(gè)組名,判斷當(dāng)前是否為新的組
private boolean isFirstInGroup(int pos) {
 if (pos == 0) {
 return true;
 } else {
 String prevGroupId = getGroupName(pos - 1);
 String groupId = getGroupName(pos);
 return !TextUtils.equals(prevGroupId, groupId);
 }
}

通過(guò)position,對(duì)比上一個(gè)省份名稱(chēng),判斷當(dāng)前省是否為第一個(gè)

重寫(xiě)getItemOffsets方法,為懸浮欄預(yù)留空間

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
 super.getItemOffsets(outRect, view, parent, state);
 int pos = parent.getChildAdapterPosition(view);
 String groupId = getGroupName(pos);
 if (groupId == null) return;
 //只有是同一組的第一個(gè)才顯示懸浮欄
 if (pos == 0 || isFirstInGroup(pos)) {
 outRect.top = mGroupHeight;
 }
}

只有第一個(gè)Item或者新的省份才為懸浮欄預(yù)留空間

重寫(xiě)onDrawOver方法(重點(diǎn))

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
final int itemCount = state.getItemCount();
final int childCount = parent.getChildCount();
final int left = parent.getLeft() + parent.getPaddingLeft();
final int right = parent.getRight() - parent.getPaddingRight();
String preGroupName;  //標(biāo)記上一個(gè)item對(duì)應(yīng)的Group
String currentGroupName = null;  //當(dāng)前item對(duì)應(yīng)的Group
for (int i = 0; i < childCount; i++) {
 View view = parent.getChildAt(i);
 int position = parent.getChildAdapterPosition(view);
 preGroupName = currentGroupName;
 currentGroupName = getGroupName(position);
 if (currentGroupName == null || TextUtils.equals(currentGroupName, preGroupName))
  continue;
 int viewBottom = view.getBottom();
 float top = Math.max(mGroupHeight, view.getTop());//top 決定當(dāng)前頂部第一個(gè)懸浮Group的位置
 if (position + 1 < itemCount) {
  //獲取下個(gè)GroupName
  String nextGroupName = getGroupName(position + 1);
  //下一組的第一個(gè)View接近頭部
  if (!currentGroupName.equals(nextGroupName) && viewBottom < top) {
   top = viewBottom;
  }
 }
 //根據(jù)top繪制group
 c.drawRect(left, top - mGroupHeight, right, top, mGroutPaint);
 Paint.FontMetrics fm = mTextPaint.getFontMetrics();
 //文字豎直居中顯示
 float baseLine = top - (mGroupHeight - (fm.bottom - fm.top)) / 2 - fm.bottom;
 c.drawText(currentGroupName, left + mLeftMargin, baseLine, mTextPaint);
 }
}

通過(guò)變量preGroupId和currentGroupId來(lái)保存當(dāng)前分組名和上一個(gè)分組名。當(dāng)前Item與上一個(gè)Item為同一個(gè)分組時(shí),跳過(guò)該Item的繪制。

其中代碼:

float top = Math.max(mGroupHeight, view.getTop());

根據(jù)當(dāng)前Item的位置確定繪制分組的位置。top將在mGroupHeight和view.getTop()中取最大值,也就是說(shuō)top將不會(huì)小于mGroupHeight,這樣就能實(shí)現(xiàn)吸頂效果。

其中代碼:

if (!currentGroupName.equals(nextGroupName) && viewBottom < top) {
  top = viewBottom;
 }

當(dāng)下個(gè)分組的頂部(當(dāng)前Item的底部viewBottom可近似認(rèn)為下個(gè)Item的頂部)距離RecyclerView頂部小于top時(shí),偏移當(dāng)前分組位置。實(shí)現(xiàn)下一組上滑時(shí)候,當(dāng)前分組上移;上一組下滑的時(shí)候,當(dāng)前分組下移。
最后計(jì)算baseLine,并繪制背景和文字。

到目前為止,一個(gè)帶有懸浮功能的列表就實(shí)現(xiàn)了。

(詳細(xì)代碼見(jiàn)底部鏈接)

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

進(jìn)階

當(dāng)我們利用ItemDecoration實(shí)現(xiàn)文字的懸浮的時(shí)候,是不是還可以搞點(diǎn)事情~ ~我有個(gè)大膽的想法
只有文字的懸浮怎么行!我還希望可以再來(lái)個(gè)icon?再來(lái)幾個(gè)TextView?來(lái)個(gè)自定義布局?那就來(lái)個(gè)自定義布局吧。

實(shí)現(xiàn)

實(shí)現(xiàn)的原理跟上面一樣,由于需要自定義布局,所以需要在接口中添加一個(gè)獲取View的方法。

定義PowerGroupListener

public interface PowerGroupListener {
 String getGroupName(int position);
 View getGroupView(int position);
}

相比之前,多了個(gè)getGroupView方法,用來(lái)獲取View。

在onDrawOver中繪制

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
 ...
 for (int i = 0; i < childCount; i++) {
  ...
  //根據(jù)position獲取View
  View groupView = mGroupListener.getGroupView(position);
  if (groupView == null) return;
  ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, mGroupHeight);
groupView.setLayoutParams(layoutParams);
  groupView.setDrawingCacheEnabled(true);
  groupView.measure(
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  //指定高度、寬度的groupView
  groupView.layout(0, 0, right, mGroupHeight);
  groupView.buildDrawingCache();
  Bitmap bitmap = groupView.getDrawingCache();
  c.drawBitmap(bitmap, left, top - mGroupHeight, null);
 }
}

在原來(lái)的基礎(chǔ)上做了點(diǎn)修改,通過(guò)接口的getGroupView方法獲取需要繪制的分組View,將得到的View繪制到指定位置。

效果:

Android RecyclerView打造懸浮效果的實(shí)現(xiàn)代碼

(詳細(xì)代碼見(jiàn)底部鏈接)

源碼鏈接

已封裝成庫(kù),歡迎來(lái)提Issues

repositories {
 jcenter()// If not already there
}
dependencies {
 compile 'com.gavin.com.library:stickyDecoration:1.0.2'
}

詳細(xì)用法級(jí)源碼請(qǐng)看Github

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI