1. 程式人生 > >Android之RecyclerView簡單使用(三)

Android之RecyclerView簡單使用(三)

圖片 vertica pack listview sdn art code offset ddc

使用過ListView滴小夥伴都知道。ListView有這樣一個屬性android:divider,用來設置每一個item之間切割線滴屬性。問題來了,那麽RecyclerView這個控件有沒有這個屬性呢,嘗試了一下,沒有這個屬性。可是RecyclerView有這種方法mLinearRecycler.addItemDecoration(),用來加入切割線滴。以下是這部分源代碼:

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); } @Deprecated public void getItemOffsets(Rect outRect, int itemPosition,RecyclerView parent) { outRect.set(0, 0, 0, 0); } public void getItemOffsets(Rect outRect, View view,RecyclerView parent, State state) { getItemOffsets(outRect, ((LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent); } }

RecyclerView在調用addItemDecoration()滴時候就會去繪制decoration,通過源代碼我們會發現,會調用裏面的onDraw()和onDrawOver()方法。而getItemOffsets(),是為每一個Item設置一定的偏移量。主要用於繪制decorator。

以下我們來看一下該類的實現,代碼例如以下:

    package recyclerview.hy.com.myrecyclerview.decoration;

    import android.content.Context;
    import android.graphics.Canvas;
    import
android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import recyclerview.hy.com.myrecyclerview.R; /** * Created by Ying on 2016/2/16. */ public class ListItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDrawable; private final static int DEFAULT_ORENTATION = LinearLayoutManager.VERTICAL; private int mOrientation; public ListItemDecoration(Context context, int orientation) { if (orientation != LinearLayoutManager.HORIZONTAL && orientation != LinearLayoutManager.VERTICAL) { //方法一:假設沒設置布局顯示方向,默認設置為垂直方向 this.mOrientation = DEFAULT_ORENTATION; //方法二:拋出異常,提示設置布局方向 // throw new IllegalArgumentException("invalid orientation"); } else { this.mOrientation = orientation; } //設置切割線樣式 mDrawable = context.getResources().getDrawable(R.drawable.divider); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { if (mOrientation == LinearLayoutManager.HORIZONTAL) { drawHorizontal(c, parent); } else { drawVertical(c, parent); } } private 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); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams(); int left = child.getRight() + params.rightMargin; int right = left + mDrawable.getIntrinsicHeight(); mDrawable.setBounds(left, top, right, bottom); mDrawable.draw(c); } } private 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); android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + mDrawable.getIntrinsicHeight(); mDrawable.setBounds(left, top, right, bottom); mDrawable.draw(c); } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if (mOrientation == DEFAULT_ORENTATION) { outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight()); } else { outRect.set(0, 0, mDrawable.getIntrinsicWidth(), 0); } } }

這樣就實現了切割線的類,我們僅僅要在Activity中加入這樣一句話就可以:

mLinearRecycler.addItemDecoration(new ListItemDecoration(this, LinearLayoutManager.VERTICAL));

看一下效果圖:
技術分享
假設你想設置其他樣式滴切割線僅僅需在這句話進行改動:

 mDrawable = context.getResources().getDrawable(R.drawable.divider);

關於RecyclerView實現GridView和瀑布流效果就不在貼代碼了。這些代碼demo裏面有。

看一下效果圖:
GridView:
技術分享
瀑布流:
技術分享
關於瀑布流背景點擊變色,假設有疑惑,能夠參考我滴還有一篇博客Android之簡單改變button顏色方案。


項目代碼:demo下載地址
如有疑問。記得給我留言哦~技術交流嘛~~喜歡滴,頂一下唄!

Android之RecyclerView簡單使用(三)