Android仿微信通訊錄打造帶懸停頭部的分組列表(上)
一 概述
本文是Android導(dǎo)航分組列表系列上,因時(shí)間和篇幅原因分上下,最終上下合璧,完整版效果如下:
上部殘卷效果如下:兩個(gè)ItemDecoration,一個(gè)實(shí)現(xiàn)懸停頭部分組列表功能,一個(gè)實(shí)現(xiàn)分割線(官方demo)
網(wǎng)上關(guān)于實(shí)現(xiàn)帶懸停分組頭部的列表的方法有很多,像我看過(guò)有主席的自定義ExpandListView實(shí)現(xiàn)的,也看過(guò)有人用一個(gè)額外的父布局里面套 RecyclerView/ListView+一個(gè)頭部View(位置固定在父布局上方)實(shí)現(xiàn)的。
對(duì)于以上解決方案,有以下幾點(diǎn)個(gè)人覺得不好的地方:
1. 現(xiàn)在RecyclerView是主流
2. 在RecyclerView外套一個(gè)父布局總歸是增加布局層級(jí),容易o(hù)verdraw,顯得不夠優(yōu)雅。
3. item布局實(shí)現(xiàn)帶這種分類頭部的方法有兩種,一種是把分類頭部當(dāng)做一種itemViewtype(麻煩),另一種是每個(gè)Item布局都包含了分類頭部的布局,代碼里根據(jù)postion等信息動(dòng)態(tài)Visible,Gone頭部(布局冗余,item效率降低)。
況且Google為我們提供了ItemDecoration,它本身就是用來(lái)修飾RecyclerView里的Item的,它的getItemOffsets() onDraw()方法用于為Item分類頭部留出空間和繪制(解決缺點(diǎn)3),它的onDrawOver()方法用于繪制懸停的頭部View(解決缺點(diǎn)2)。
而且更重要的是,ItemDecoration出來(lái)這么久了,你還不用它?
本文就利用ItemDecoration 打造 分組列表,并配有懸停頭部功能。
亮點(diǎn)預(yù)覽:添加多個(gè)ItemDecoration、它們的執(zhí)行順序、ItemDecoration方法執(zhí)行順序、ItemDecoration和RecyclerView的繪制順序
二 使用ItemDecoration
用法:為RecyclerViewPool添加一個(gè)或多個(gè)ItemDecoration
//如果add多個(gè),那么按照先后順序,依次渲染。 mRv.addItemDecoration(mDecoration = new TitleItemDecoration(this, mDatas)); mRv.addItemDecoration(new TitleItemDecoration2(this,mDatas)); mRv.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL_LIST));
為RecyclerView添加ItemDecoration只要這么一句addItemDecoration(),
它有兩個(gè)同名重載方法:
addItemDecoration(ItemDecoration decor) 常用,(按照add順序,依次渲染ItemDecoration)
addItemDecoration(ItemDecoration decor, int index) add一個(gè)ItemDecoration,并為它指定順序
上來(lái)就高能,別的講解RecyclerView的文章一般都是對(duì)ItemDecoration一筆帶過(guò),用的Demo一般也都是官方的DividerItemDecoration類,更別提還添加多個(gè)ItemDecoration了。其實(shí)我也是昨天寫Demo的時(shí)候才發(fā)現(xiàn)這個(gè)方法,點(diǎn)進(jìn)去查看了一下源碼:
public void addItemDecoration(ItemDecoration decor) { addItemDecoration(decor, -1); } public void addItemDecoration(ItemDecoration decor, int index) { if (mLayout != null) { mLayout.assertNotInLayoutOrScroll("Cannot add item decoration during a scroll or" + " layout"); } if (mItemDecorations.isEmpty()) { setWillNotDraw(false); } if (index < 0) { mItemDecorations.add(decor); } else { mItemDecorations.add(index, decor); } markItemDecorInsetsDirty(); requestLayout(); }
老套路:我們最常用的單參數(shù)方法 內(nèi)部調(diào)用了雙參數(shù)方法,并把index 傳入-1。
我們add的ItemDecoration 都存儲(chǔ)在RecyclerView類的mItemDecorations變量里,
這個(gè)變量就是一個(gè)ArrayList,定義如下
private final ArrayList<ItemDecoration> mItemDecorations = new ArrayList<>();
三 ItemDecoration方法介紹和編寫
常用(全部)方法:
按照在RecyclerView中它們被調(diào)用的順序排列:
1. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
2. public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
3. public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
這個(gè)三個(gè)方法也是繼承一個(gè)ItemDecoration必須實(shí)現(xiàn)的三個(gè)方法。(其實(shí)ItemDecoration里除了@Deprecated 的方法 也就它們?nèi)?,?/p>
方法一的編寫
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state):
我們需要利用 parent和state變量,來(lái)獲取需要的輔助信息,例如postion, 最終調(diào)用outRect.set(int left, int top, int right, int bottom)方法,設(shè)置四個(gè)方向上 需要為ItemView設(shè)置padding的值。
下圖我覺得很經(jīng)典:摘自(https://blog.piasy.com/2016/03/26/Insight-Android-RecyclerView-ItemDecoration/?utm_source=tuicool&utm_medium=referral)向作者表示感謝。如作者不許我轉(zhuǎn)圖,煩請(qǐng)聯(lián)系我刪除
本文的 實(shí)體bean如下編寫:
/** * Created by zhangxutong . * Date: 16/08/28 */ public class CityBean { private String tag;//所屬的分類(城市的漢語(yǔ)拼音首字母) private String city; public CityBean(String tag, String city) { this.tag = tag; this.city = city; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
getItemOffsets方法 如下:
通過(guò)parent獲取postion信息,通過(guò)postion拿到數(shù)據(jù)里的每個(gè)bean里的分類,因?yàn)閿?shù)據(jù)集已經(jīng)有序,如果與前一個(gè)分類不一樣,說(shuō)明是一個(gè)新的分類,則需要繪制頭部outRect.set(0, mTitleHeight, 0, 0);,否則不需要outRect.set(0, 0, 0, 0);。
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 outRect.set(0, mTitleHeight, 0, 0); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { outRect.set(0, mTitleHeight, 0, 0);//不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title } else { outRect.set(0, 0, 0, 0); } } } }
--------------------------------------------------------------------------------
方法二的編寫
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
我們需要利用 parent和state變量,來(lái)獲取需要的輔助信息,例如繪制的上下左右,childCount, childView等。。最終利用c調(diào)用Canvas的方法來(lái)繪制出我們想要的UI。會(huì)自定義View就會(huì)寫本方法~
onDraw繪制出的內(nèi)容是在ItemView下層,雖然它可以繪制超出getItemOffsets()里的Rect區(qū)域,但是超出區(qū)域最終不會(huì)顯示,但被ItemView覆蓋的區(qū)域會(huì)產(chǎn)生OverDraw。
本文如下編寫:通過(guò)parent獲取繪制UI的 left和right以及childCount, 遍歷childView,根據(jù)childView的postion,和方法一中的判斷方法一樣,來(lái)決定是否繪制分類Title區(qū)域:
分類繪制title的方法就是自定義View的套路,根據(jù)確定的上下左右范圍先drawRect繪制一個(gè)背景,然后drawText繪制文字。
(不會(huì)自定義View的可參考郭神 洋神 文章:
http://blog.csdn.net/lmj623565791/article/details/24252901 http://blog.csdn.net/guolin_blog/article/details/17357967)。
@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); int position = params.getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 drawTitleArea(c, left, right, child, params, position); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { //不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title drawTitleArea(c, left, right, child, params, position); } else { //none } } } } } /** * 繪制Title區(qū)域背景和文字的方法 * * @param c * @param left * @param right * @param child * @param params * @param position */ private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {//最先調(diào)用,繪制在最下層 mPaint.setColor(COLOR_TITLE_BG); c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(mDatas.get(position).getTag(), 0, mDatas.get(position).getTag().length(), mBounds); c.drawText(mDatas.get(position).getTag(), child.getPaddingLeft(), child.getTop() - params.topMargin - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); }
寫完 12 方法,就已經(jīng)完成了分類列表title的繪制,方法3實(shí)現(xiàn)頂部懸停title效果:GO
--------------------------------------------------------------------------------
方法三的編寫
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state):
和 onDraw()方法類似, 我們需要利用 parent和state變量,來(lái)獲取需要的輔助信息,例如繪制的上下左右,position, childView等。。最終利用c調(diào)用Canvas的方法來(lái)繪制出我們想要的UI。同樣是會(huì)自定義View就會(huì)寫本方法~
onDrawOver繪制出的內(nèi)容是在RecyclerView的最上層,會(huì)遮擋住ItemView,So天生自帶懸停效果,用來(lái)繪制懸停View再好不過(guò)。
本文如下編寫:首先通過(guò)parent獲取LayoutManager(由于懸停分組列表的特殊性,寫死了是LinearLayoutManger),然后獲取當(dāng)前第一個(gè)可見itemView以及postion,以及它所屬的分類title(tag),然后繪制懸停View的背景和文字(tag),可參考方法2里的書寫,大同小異。
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {//最后調(diào)用 繪制在最上層 int pos = ((LinearLayoutManager)(parent.getLayoutManager())).findFirstVisibleItemPosition(); String tag = mDatas.get(pos).getTag(); View child = parent.getChildAt(pos); mPaint.setColor(COLOR_TITLE_BG); c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(tag, 0, tag.length(), mBounds); c.drawText(tag, child.getPaddingLeft(), parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); }
至此,我們的 帶懸停頭部的分組列表的ItemDecoration就編寫完畢了,完整代碼如下:
四 分類title ItemDecoration完整代碼:
/** * 有分類title的 ItemDecoration * Created by zhangxutong . * Date: 16/08/28 */ public class TitleItemDecoration extends RecyclerView.ItemDecoration { private List<CityBean> mDatas; private Paint mPaint; private Rect mBounds;//用于存放測(cè)量文字Rect private int mTitleHeight;//title的高 private static int COLOR_TITLE_BG = Color.parseColor("#FFDFDFDF"); private static int COLOR_TITLE_FONT = Color.parseColor("#FF000000"); private static int mTitleFontSize;//title字體大小 public TitleItemDecoration(Context context, List<CityBean> datas) { super(); mDatas = datas; mPaint = new Paint(); mBounds = new Rect(); mTitleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics()); mTitleFontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()); mPaint.setTextSize(mTitleFontSize); mPaint.setAntiAlias(true); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); int position = params.getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 drawTitleArea(c, left, right, child, params, position); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { //不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title drawTitleArea(c, left, right, child, params, position); } else { //none } } } } } /** * 繪制Title區(qū)域背景和文字的方法 * * @param c * @param left * @param right * @param child * @param params * @param position */ private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {//最先調(diào)用,繪制在最下層 mPaint.setColor(COLOR_TITLE_BG); c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin, mPaint); mPaint.setColor(COLOR_TITLE_FONT); /* Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;*/ mPaint.getTextBounds(mDatas.get(position).getTag(), 0, mDatas.get(position).getTag().length(), mBounds); c.drawText(mDatas.get(position).getTag(), child.getPaddingLeft(), child.getTop() - params.topMargin - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {//最后調(diào)用 繪制在最上層 int pos = ((LinearLayoutManager)(parent.getLayoutManager())).findFirstVisibleItemPosition(); String tag = mDatas.get(pos).getTag(); View child = parent.getChildAt(pos); mPaint.setColor(COLOR_TITLE_BG); c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(tag, 0, tag.length(), mBounds); c.drawText(tag, child.getPaddingLeft(), parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 outRect.set(0, mTitleHeight, 0, 0); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { outRect.set(0, mTitleHeight, 0, 0);//不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title } else { outRect.set(0, 0, 0, 0); } } } } } /** * 有分類title的 ItemDecoration * Created by zhangxutong . * Date: 16/08/28 */ public class TitleItemDecoration extends RecyclerView.ItemDecoration { private List<CityBean> mDatas; private Paint mPaint; private Rect mBounds;//用于存放測(cè)量文字Rect private int mTitleHeight;//title的高 private static int COLOR_TITLE_BG = Color.parseColor("#FFDFDFDF"); private static int COLOR_TITLE_FONT = Color.parseColor("#FF000000"); private static int mTitleFontSize;//title字體大小 public TitleItemDecoration(Context context, List<CityBean> datas) { super(); mDatas = datas; mPaint = new Paint(); mBounds = new Rect(); mTitleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics()); mTitleFontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()); mPaint.setTextSize(mTitleFontSize); mPaint.setAntiAlias(true); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); int position = params.getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 drawTitleArea(c, left, right, child, params, position); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { //不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title drawTitleArea(c, left, right, child, params, position); } else { //none } } } } } /** * 繪制Title區(qū)域背景和文字的方法 * * @param c * @param left * @param right * @param child * @param params * @param position */ private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {//最先調(diào)用,繪制在最下層 mPaint.setColor(COLOR_TITLE_BG); c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin, mPaint); mPaint.setColor(COLOR_TITLE_FONT); /* Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;*/ mPaint.getTextBounds(mDatas.get(position).getTag(), 0, mDatas.get(position).getTag().length(), mBounds); c.drawText(mDatas.get(position).getTag(), child.getPaddingLeft(), child.getTop() - params.topMargin - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {//最后調(diào)用 繪制在最上層 int pos = ((LinearLayoutManager)(parent.getLayoutManager())).findFirstVisibleItemPosition(); String tag = mDatas.get(pos).getTag(); View child = parent.getChildAt(pos); mPaint.setColor(COLOR_TITLE_BG); c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(tag, 0, tag.length(), mBounds); c.drawText(tag, child.getPaddingLeft(), parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); //我記得Rv的item position在重置時(shí)可能為-1.保險(xiǎn)點(diǎn)判斷一下吧 if (position > -1) { if (position == 0) {//等于0肯定要有title的 outRect.set(0, mTitleHeight, 0, 0); } else {//其他的通過(guò)判斷 if (null != mDatas.get(position).getTag() && !mDatas.get(position).getTag().equals(mDatas.get(position - 1).getTag())) { outRect.set(0, mTitleHeight, 0, 0);//不為空 且跟前一個(gè)tag不一樣了,說(shuō)明是新的分類,也要title } else { outRect.set(0, 0, 0, 0); } } } } }
五 一些ItemDecoration的相關(guān)補(bǔ)充姿勢(shì)
1. 多個(gè)ItemDecoration,以及它們的繪制順序。
就像第二節(jié)中的用法提到的,可以為一個(gè)RecyclerView添加多個(gè)ItemDecoration,那么多個(gè)ItemDecoration的繪制順序是什么呢:我們看看源碼吧:
第二節(jié)中提到,多個(gè)ItemDecoration最終是存儲(chǔ)在RecyclerView里的mItemDecorations(ArrayList)變量中,那我們就去RecyclerView的 源碼里搜一搜,看看哪些地方用到了mItemDecorations。
發(fā)現(xiàn)在draw()和onDraw()方法里:按照在mItemDecorations里的postion順序,依次調(diào)用了每個(gè)ItemDecoration的onDrawOver和onDraw方法。所以后添加的ItemDecoration,如果和前面的ItemDecoration的繪制區(qū)域有重合的地方,會(huì)遮蓋住前面的ItemDecoration(OverDraw)。
@Override public void draw(Canvas c) { super.draw(c); final int count = mItemDecorations.size(); for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDrawOver(c, this, mState); } @Override public void onDraw(Canvas c) { super.onDraw(c); final int count = mItemDecorations.size(); for (int i = 0; i < count; i++) { mItemDecorations.get(i).onDraw(c, this, mState); } }
2. ItemDecoration和RecyclerView的Item的繪制順序。
在介紹ItemDecoration的三個(gè)方法時(shí),我們提到過(guò)結(jié)論:
ItemDecoration的onDraw最先調(diào)用,繪制在最底層, 其上再繪制ItemView 中間層, 再上調(diào)用ItemDecoration的onDrawOver,繪制在最上層。
理由:
由上面代碼可見, RecyclerView的draw()方法中,在super.draw(c)方法調(diào)用完后,才調(diào)用mItemDecorations.get(i).onDrawOver(c, this, mState); 而super.draw(c)方法就是直接調(diào)用View的public void draw(Canvas canvas) 方法,如下所示:
其中又先調(diào)用了View(RecyclerView)的onDraw()方法,
在RecyclerView的onDraw()方法中,會(huì)調(diào)用mItemDecorations.get(i).onDraw(c, this, mState);
所以onDraw最先調(diào)用,繪制在最底層
后調(diào)用了View(ViewGroup)的dispatchDraw(canvas)方法;
在ViewGroup的dispatchDraw(canvas)方法里,會(huì)執(zhí)行 drawChild(Canvas canvas, View child, long drawingTime)方法,繪制每個(gè)itemView。
所以ItemView繪制在中間層
最后super.draw(c)走完,調(diào)用mItemDecorations.get(i).onDrawOver(c, this, mState);
所以再上調(diào)用ItemDecoration的onDrawOver,繪制在最上層。 (從方法名字也可以看出哈)
View的draw()方法如下,
/** * This method is called by ViewGroup.drawChild() to have each child view draw itself. * * This is where the View specializes rendering behavior based on layer type, * and hardware acceleration. */ boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) { ............省略 // Step 3, draw the content if (!dirtyOpaque) onDraw(canvas); // Step 4, draw the children dispatchDraw(canvas);
六 完整代碼地址
CSDN代碼上傳中
歡迎光臨我的github下載上下合集demo:喜歡的隨手點(diǎn)個(gè)star 哈~
https://github.com/mcxtzhang/Demos/tree/master/itemdecorationdemo
master分支為上部殘篇,sideBar分支為上下合璧完整篇。
七 總結(jié)
本文是我第一次用MarkDown編寫博客,感覺一個(gè)字爽。
RecyclerView相關(guān)的各個(gè)類,個(gè)個(gè)是寶,每一次探索都覺得如獲至寶, 感覺利用ItemDecoration可以干很多事,可惜ItemDecoration貌似不能接受到用戶的點(diǎn)擊事件~要不我右側(cè)導(dǎo)航欄都想用ItemDecoration實(shí)現(xiàn)了。
關(guān)于可以add多個(gè)ItemDecoration這一點(diǎn),想了一下,覺得很精妙,這是一種很好的設(shè)計(jì)思想,多個(gè)ItemDecoration各司其職,如本文,采用官方ItemDecoration作分割線,自己又寫一個(gè)ItemDecoration作分類title和分類title相關(guān)的懸停title。用時(shí)根據(jù)需要,選擇任意數(shù)量的“裝飾品”ItemDecoration,來(lái)豐富你的RecyclerView。可能我的low常規(guī)思想還是一個(gè)XXX類,使用時(shí)如果擴(kuò)充功能,需要extends and code~但這樣不同的功能就太耦合了,不利于復(fù)用。畢竟 “組合大于繼承”。
這一周亞歷山大,工作上的事很多,下篇原本打算明天寫的,可能要挪到周末了。
心急的朋友可以去我的github上 sideBar分支看,就是在本文的基礎(chǔ)上,組合一個(gè)側(cè)邊欄自定義View,然后利用TinyPinyin(https://github.com/promeG/TinyPinyin),取數(shù)據(jù)源的拼音,然后利用拼音順序排序數(shù)據(jù)源,set給Adapter,set給側(cè)邊欄,監(jiān)聽側(cè)邊欄的Item切換,在回調(diào)方法里,調(diào)用RecyclerView的scrollToPositionWithOffset(int position, int offset) 方法,滑動(dòng)RecyclerView到指定位置~。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用adb命令向Android模擬器中導(dǎo)入通訊錄聯(lián)系人的方法
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號(hào)碼和姓名
- Android個(gè)人手機(jī)通訊錄開發(fā)詳解
- Android實(shí)現(xiàn)仿通訊錄側(cè)邊欄滑動(dòng)SiderBar效果代碼
- Android破解微信獲取聊天記錄和通訊錄信息(靜態(tài)方式)
- android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置)
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
- Android實(shí)現(xiàn)通訊錄功能
相關(guān)文章
Android3.0 ActionBar導(dǎo)航標(biāo)題欄使用解析
這篇文章主要為大家詳細(xì)解析了Android3.0 ActionBar導(dǎo)航標(biāo)題欄的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐
本篇文章主要介紹了淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問(wèn)題
這篇文章主要介紹了解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Android攔截并獲取WebView內(nèi)部POST請(qǐng)求參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Android攔截并獲取WebView內(nèi)部POST請(qǐng)求參數(shù) 的實(shí)現(xiàn)方法,本文通過(guò)兩種方案給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12Android PowerManagerService省電模式策略控制
這篇文章主要介紹了Android PowerManagerService省電模式策略控制,本文基于前兩篇文章的基礎(chǔ)介紹展開詳情,感興趣的小伙伴可以參考一下2022-08-08android listview實(shí)現(xiàn)新聞列表展示效果
這篇文章主要為大家詳細(xì)介紹了android listview實(shí)現(xiàn)新聞列表展示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03圖解Windows環(huán)境下Android Studio安裝和使用教程
這篇文章主要介紹了圖解Windows環(huán)境下Android Studio安裝和使用教程的相關(guān)資料,需要的朋友可以參考下2015-12-12