Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

拿掉 RecyclerView 底部多餘分割線的做法

編輯:關於 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() 判斷。

以上就是本文全部內容,希望對你的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved