編輯:關於 Android 程式設計
RecyclerView 是 Android 5.0 Material Design 中的核心組件之一。相比 ListView,它通過更低耦合的方式複用 ViewHolder,並能輕鬆實現列表、網格與瀑布流。不過它並不內置分割線,需要通過 ItemDecoration 自己實現。本文封裝一個「萬能分割線」,支持默認樣式、自定義圖片、自定義高度與顏色三種用法。

默認分割線(高度 2px、灰色):
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL));
自定義分割線圖片:
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));
自定義高度與顏色:
mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.divide_gray_color)));
public class RecycleViewDivider extends RecyclerView.ItemDecoration { private Paint mPaint; private Drawable mDivider; private int mDividerHeight = 2; private int mOrientation; private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; public RecycleViewDivider(Context context, int orientation) { if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) { throw new IllegalArgumentException("請輸入正確的參數!"); } mOrientation = orientation; final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); } public RecycleViewDivider(Context context, int orientation, int drawableId) { this(context, orientation); mDivider = ContextCompat.getDrawable(context, drawableId); mDividerHeight = mDivider.getIntrinsicHeight(); } public RecycleViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) { this(context, orientation); mDividerHeight = dividerHeight; mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(dividerColor); mPaint.setStyle(Paint.Style.FILL); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.set(0, 0, 0, mDividerHeight); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); if (mOrientation == LinearLayoutManager.VERTICAL) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } private void drawHorizontal(Canvas canvas, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getMeasuredWidth() - parent.getPaddingRight(); final int childSize = parent.getChildCount(); for (int i = 0; i < childSize; i++) { final View child = parent.getChildAt(i); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + layoutParams.bottomMargin; final int bottom = top + mDividerHeight; if (mDivider != null) { mDivider.setBounds(left, top, right, bottom); mDivider.draw(canvas); } if (mPaint != null) { canvas.drawRect(left, top, right, bottom, mPaint); } } } private void drawVertical(Canvas canvas, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom(); final int childSize = parent.getChildCount(); for (int i = 0; i < childSize; i++) { final View child = parent.getChildAt(i); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + layoutParams.rightMargin; final int right = left + mDividerHeight; if (mDivider != null) { mDivider.setBounds(left, top, right, bottom); mDivider.draw(canvas); } if (mPaint != null) { canvas.drawRect(left, top, right, bottom, mPaint); } } } }
這個分割線通過 getItemOffsets 留出間距,再在 onDraw 中根據方向繪制,同時兼顧了自定義 Drawable 與自定義 Paint 兩條繪制路徑,因此能適配絕大多數列表場景。以上就是本文的全部內容,希望對大家有所幫助。
Android 錄屏生成 GIF 動圖完整流程
為何選擇 GIF開發 App 後,常需要製作簡短展示動圖。相較於影片,GIF 更輕量、更容易嵌入文章或簡報,也適合展示十幾秒的操作流程。整體思路通常分為兩步:先錄製操作影
Android 觸摸事件傳遞與處理
事件傳遞概念Android 觸摸事件從 Activity 向下分發,再由子 View 向上處理。常用方法包括 dispatchTouchEvent、onIntercept
Android ViewPager 與 Fragment 結合使用
ViewPager 的基本結構ViewPager 常用來實作可左右滑動的頁面切換效果。若希望每一頁顯示不同內容,通常需要搭配 Adapter,並把每一頁設計成 Fragm
Android ViewStub 延遲加載解析:用源碼視角理解輕量級佔位元件
為什麼需要延遲加載在 Android 介面中,有些 View 並不是一開始就需要顯示,而是在特定條件下才會出現。若將這些元件全部预先加入版面,會增加記憶體與渲染負擔。Vi