編輯:關於 Android 程式設計
最近用 RecyclerView 顯示縱向列表,加上系統預設的分割線,結果發現最後一筆下方也多了一條線,視覺上很突兀。
試了不少繞路的方法都不理想,最後決定直接去看 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");
}
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);
}
}
}
問題定位
我用的是縱向列表,走的是 drawVertical()。看程式就明白了:迴圈從 0 跑到 childCount - 1,對每一個 item 都在其下方畫線,最後一筆自然也被畫上。
修正方式很直接——把迴圈改成少跑一次:
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);
}
}
怎麼用
由於這個類在 support 套件裡,我們無法直接改源碼,因此做法是自己複製一份並改名(例如 NoLastDividerItemDecoration),套用上面的修改,然後在設定時改用自訂類別:
mRecyclerView.addItemDecoration(new NoLastDividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
另一個更簡潔的選擇
如果你只是不想要最後一條線,其實也可以不改 ItemDecoration,直接在 item 版面配置加上底部 margin 或自帶分隔線,並把 RecyclerView 的 divider 設為 null,改由 item 自己畫。這樣做的好處是線的樣式(顏色、粗細、左右縮排)更好控制,也不必為了不同情境維護多個 Decoration 類別。
另外提醒:getItemOffsets() 仍會為最後一項保留高度,因此底部會留下一小段空白。若在意這點,可在 getItemOffsets() 中對 itemPosition == itemCount - 1 回傳 0,或改用 parent.getChildAdapterPosition() 判斷。
以上就是本文全部內容,希望對你的學習有所幫助。
QQ火炬火種怎麼領 明星派發火種預約教學
里約奧運會即將開幕,這兩天大家應該都玩上了騰訊的新玩法——QQ火炬。如今又有新花樣上線:QQ邀請了關曉彤、蔣勁夫、田亮、周冬雨等一眾明星派發火種。那麼火種怎麼獲得?如何預
Android 百度地圖 SDK v3.0.0(四):集成離線地圖功能
一直覺得地圖應用支持離線地圖很重要。對於地處 2G 網絡、流量緊張的用戶來說,流量來之不易,且用且珍惜。 官方開發指南針對離線地圖提供了兩種方案: 第一,手動導入:先將從
Launcher3 壁紙機制解析:從選擇、裁切到動態壁紙
壁紙是系統能力 Launcher 的壁紙設定並非 Launcher 獨有,而是透過系統 WallpaperManager 完成。因此,不只是 Launcher,許多第三方
Android N 開發指南:環境搭建與 20 項新特性盤點
一、前言 如果英文不錯,建議直接看官方文檔,官網底部也提供多語言切換:https://developer.android.com/preview/api-overview