欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

RecyclerView消除底部分割線的方法

 更新時(shí)間:2020年07月23日 11:06:14   作者:掌握當(dāng)下  
這篇文章主要為大家詳細(xì)介紹了RecyclerView消除底部分割線的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近遇到一個(gè)問(wèn)題,用RecyclerView顯示數(shù)據(jù),縱向列表顯示,添加默認(rèn)分割線。

問(wèn)題是:底部也會(huì)顯示分割線,這很影響美觀。

怎么解決這個(gè)問(wèn)題呢?我想了很多辦法,毫無(wú)頭緒。。。

最后,查看默認(rèn)分割線的類(lèi)DividerItemDecoration的源碼:

public class DividerItemDecoration extends ItemDecoration {
 private static final int[] ATTRS = new int[]{16843284};
 public static final int HORIZONTAL_LIST = 0;
 public static final int VERTICAL_LIST = 1;
 private Drawable mDivider;
 private int mOrientation;

 public DividerItemDecoration(Context context, int orientation) {
  TypedArray a = context.obtainStyledAttributes(ATTRS);
  this.mDivider = a.getDrawable(0);
  a.recycle();
  this.setOrientation(orientation);
 }

 public void setOrientation(int orientation) {
  if(orientation != 0 && orientation != 1) {
   throw new IllegalArgumentException("invalid orientation");
  } else {
   this.mOrientation = orientation;
  }
 }

 public void onDraw(Canvas c, RecyclerView parent) {
  if(this.mOrientation == 1) {
   this.drawVertical(c, parent);
  } else {
   this.drawHorizontal(c, parent);
  }

 }

 public void drawVertical(Canvas c, RecyclerView parent) {
  int left = parent.getPaddingLeft();
  int right = parent.getWidth() - parent.getPaddingRight();
  int childCount = parent.getChildCount();

  for(int i = 0; i < childCount; ++i) {
   View child = parent.getChildAt(i);
   LayoutParams params = (LayoutParams)child.getLayoutParams();
   int top = child.getBottom() + params.bottomMargin;
   int bottom = top + this.mDivider.getIntrinsicHeight();
   this.mDivider.setBounds(left, top, right, bottom);
   this.mDivider.draw(c);
  }

 }

 public void drawHorizontal(Canvas c, RecyclerView parent) {
  int top = parent.getPaddingTop();
  int bottom = parent.getHeight() - parent.getPaddingBottom();
  int childCount = parent.getChildCount();

  for(int i = 0; i < childCount; ++i) {
   View child = parent.getChildAt(i);
   LayoutParams params = (LayoutParams)child.getLayoutParams();
   int left = child.getRight() + params.rightMargin;
   int right = left + this.mDivider.getIntrinsicHeight();
   this.mDivider.setBounds(left, top, right, bottom);
   this.mDivider.draw(c);
  }

 }

 public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
  if(this.mOrientation == 1) {
   outRect.set(0, 0, 0, this.mDivider.getIntrinsicHeight());
  } else {
   outRect.set(0, 0, this.mDivider.getIntrinsicWidth(), 0);
  }

 }
}

因?yàn)槲矣玫降氖谴怪绷斜?,用到的是紅色字體處的代碼:

 public void drawVertical(Canvas c, RecyclerView parent) {
  int left = parent.getPaddingLeft();
  int right = parent.getWidth() - parent.getPaddingRight();
  int childCount = parent.getChildCount();

  for(int i = 0; i < childCount; ++i) {
   View child = parent.getChildAt(i);
   LayoutParams params = (LayoutParams)child.getLayoutParams();
   int top = child.getBottom() + params.bottomMargin;
   int bottom = top + this.mDivider.getIntrinsicHeight();
   this.mDivider.setBounds(left, top, right, bottom);
   this.mDivider.draw(c);
  }

 }

從代碼中很容易看出只要修改for循環(huán)中的內(nèi)容就可去掉底部的分割線:

 public void drawVertical(Canvas c, RecyclerView parent) {
  int left = parent.getPaddingLeft();
  int right = parent.getWidth() - parent.getPaddingRight();
  int childCount = parent.getChildCount();

  for(int i = 0; i < childCount-1; ++i) {
   View child = parent.getChildAt(i);
   LayoutParams params = (LayoutParams)child.getLayoutParams();
   int top = child.getBottom() + params.bottomMargin;
   int bottom = top + this.mDivider.getIntrinsicHeight();
   this.mDivider.setBounds(left, top, right, bottom);
   this.mDivider.draw(c);
  }

 }

因?yàn)檫@個(gè)類(lèi)我們不能直接修改,所以我們可以自定義一個(gè)類(lèi),修改相應(yīng)內(nèi)容,添加分割線的時(shí)候,使用自定義類(lèi)。

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

相關(guān)文章

最新評(píng)論