日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

RecyclerView中ItemDecoration的精妙用法,實現自定義分隔線、邊距和背景效果

來源: 責編: 時間:2024-05-11 09:16:23 227觀看
導讀ItemDecoration 是 RecyclerView 組件的一個非常有用的功能,用于添加自定義的裝飾項(如分隔線、邊距、背景等)到 RecyclerView 的每個 item 之間或周圍。recyclerView.addItemDecoration()ItemDecoration主要的三個方法:o

ItemDecoration 是 RecyclerView 組件的一個非常有用的功能,用于添加自定義的裝飾項(如分隔線、邊距、背景等)到 RecyclerView 的每個 item 之間或周圍。12G28資訊網——每日最新資訊28at.com

recyclerView.addItemDecoration()

ItemDecoration主要的三個方法:12G28資訊網——每日最新資訊28at.com

  1. onDraw(Canvas c, RecyclerView parent, RecyclerView.State state): 在 RecyclerView 的 canvas 上繪制自定義的裝飾項,通常用于繪制分隔線或背景。
  2. onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state): 與 onDraw 類似,但繪制的內容會出現在 item 的視圖之上。在 item 視圖上方繪制內容(如高亮或選擇效果),可以使用這個方法。
  3. getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state): 設置 item 的邊距。outRect 參數是一個 Rect 對象,可以設置它的 left、top、right 和 bottom 屬性來定義 item 的額外空間。這些額外的空間會用于繪制分隔線或邊距。

圖片圖片12G28資訊網——每日最新資訊28at.com

  • 圖1:代表了getItemOffsets(),可以實現類似padding的效果。
  • 圖2:代表了onDraw(),可以實現類似繪制背景的效果,內容在上面。
  • 圖3:代表了onDrawOver(),可以繪制在內容的上面,覆蓋內容。

分割線

實現分割線效果需要 getItemOffsets()和 onDraw()2個方法,首先用 getItemOffsets給item下方空出一定高度的空間(例子中是1dp),然后用onDraw繪制這個空間。12G28資訊網——每日最新資訊28at.com

public class SimpleDividerDecoration extends RecyclerView.ItemDecoration {    private int dividerHeight;    private Paint dividerPaint;    public SimpleDividerDecoration(Context context) {        dividerPaint = new Paint();        dividerPaint.setColor(context.getResources().getColor(R.color.colorAccent));        dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.divider_height);    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        super.getItemOffsets(outRect, view, parent, state);        outRect.bottom = dividerHeight;    }    @Override    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {        int childCount = parent.getChildCount();        int left = parent.getPaddingLeft();        int right = parent.getWidth() - parent.getPaddingRight();        for (int i = 0; i < childCount - 1; i++) {            View view = parent.getChildAt(i);            float top = view.getBottom();            float bottom = view.getBottom() + dividerHeight;            c.drawRect(left, top, right, bottom, dividerPaint);        }    }}

圖片圖片12G28資訊網——每日最新資訊28at.com

標簽

標簽都是覆蓋在內容之上的,可以用onDrawOver()來實現,這里簡單實現一個顏色標簽。12G28資訊網——每日最新資訊28at.com

public class LeftAndRightTagDecoration extends RecyclerView.ItemDecoration {    private int tagWidth;    private Paint leftPaint;    private Paint rightPaint;    public LeftAndRightTagDecoration(Context context) {        leftPaint = new Paint();        leftPaint.setColor(context.getResources().getColor(R.color.colorAccent));        rightPaint = new Paint();        rightPaint.setColor(context.getResources().getColor(R.color.colorPrimary));        tagWidth = context.getResources().getDimensionPixelSize(R.dimen.tag_width);    }    @Override    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {        super.onDrawOver(c, parent, state);        int childCount = parent.getChildCount();        for (int i = 0; i < childCount; i++) {            View child = parent.getChildAt(i);            int pos = parent.getChildAdapterPosition(child);            boolean isLeft = pos % 2 == 0;            if (isLeft) {                float left = child.getLeft();                float right = left + tagWidth;                float top = child.getTop();                float bottom = child.getBottom();                c.drawRect(left, top, right, bottom, leftPaint);            } else {                float right = child.getRight();                float left = right - tagWidth;                float top = child.getTop();                float bottom = child.getBottom();                c.drawRect(left, top, right, bottom, rightPaint);            }        }    }}

圖片圖片12G28資訊網——每日最新資訊28at.com

ItemDecoration組合

ItemDecoration是可以疊加的,可以將多個效果通過addItemDecoration方法疊加,將上面兩種效果疊加。12G28資訊網——每日最新資訊28at.com

recyclerView.addItemDecoration(new LeftAndRightTagDecoration(this));recyclerView.addItemDecoration(new SimpleDividerDecoration(this));

圖片圖片12G28資訊網——每日最新資訊28at.com

Section分組

定義接口用來進行數據分組和獲取首字母,重寫getItemOffsets()和onDraw()方法,并根據數據進行分組處理。12G28資訊網——每日最新資訊28at.com

public interface DecorationCallback {        long getGroupId(int position);        String getGroupFirstLine(int position);    }
public class SectionDecoration extends RecyclerView.ItemDecoration {    private static final String TAG = "SectionDecoration";    private DecorationCallback callback;    private TextPaint textPaint;    private Paint paint;    private int topGap;    private Paint.FontMetrics fontMetrics;    public SectionDecoration(Context context, DecorationCallback decorationCallback) {        Resources res = context.getResources();        this.callback = decorationCallback;        paint = new Paint();        paint.setColor(res.getColor(R.color.colorAccent));        textPaint = new TextPaint();        textPaint.setTypeface(Typeface.DEFAULT_BOLD);        textPaint.setAntiAlias(true);        textPaint.setTextSize(80);        textPaint.setColor(Color.BLACK);        textPaint.getFontMetrics(fontMetrics);        textPaint.setTextAlign(Paint.Align.LEFT);        fontMetrics = new Paint.FontMetrics();        topGap = res.getDimensionPixelSize(R.dimen.sectioned_top);//32dp    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        super.getItemOffsets(outRect, view, parent, state);        int pos = parent.getChildAdapterPosition(view);        Log.i(TAG, "getItemOffsets:" + pos);        long groupId = callback.getGroupId(pos);        if (groupId < 0) return;        if (pos == 0 || isFirstInGroup(pos)) {//同組的第一個才添加padding            outRect.top = topGap;        } else {            outRect.top = 0;        }    }    @Override    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {        super.onDraw(c, parent, state);        int left = parent.getPaddingLeft();        int right = parent.getWidth() - parent.getPaddingRight();        int childCount = parent.getChildCount();        for (int i = 0; i < childCount; i++) {            View view = parent.getChildAt(i);            int position = parent.getChildAdapterPosition(view);            long groupId = callback.getGroupId(position);            if (groupId < 0) return;            String textLine = callback.getGroupFirstLine(position).toUpperCase();            if (position == 0 || isFirstInGroup(position)) {                float top = view.getTop() - topGap;                float bottom = view.getTop();                c.drawRect(left, top, right, bottom, paint);//繪制紅色矩形                c.drawText(textLine, left, bottom, textPaint);//繪制文本            }        }    }        private boolean isFirstInGroup(int pos) {        if (pos == 0) {            return true;        } else {            long prevGroupId = callback.getGroupId(pos - 1);            long groupId = callback.getGroupId(pos);            return prevGroupId != groupId;        }    }    public interface DecorationCallback {        long getGroupId(int position);        String getGroupFirstLine(int position);    }}
recyclerView.addItemDecoration(new SectionDecoration(this, new SectionDecoration.DecorationCallback() {    @Override    public long getGroupId(int position) {        return Character.toUpperCase(dataList.get(position).getName().charAt(0));    }    @Override    public String getGroupFirstLine(int position) {        return dataList.get(position).getName().substring(0, 1).toUpperCase();    }}));

StickyHeader

頭部吸頂效果,header不動肯定是要繪制item內容之上,需要重寫onDrawOver()方法,其和Section實現一樣。12G28資訊網——每日最新資訊28at.com

public class PinnedSectionDecoration extends RecyclerView.ItemDecoration {    private static final String TAG = "PinnedSectionDecoration";    private DecorationCallback callback;    private TextPaint textPaint;    private Paint paint;    private int topGap;    private Paint.FontMetrics fontMetrics;    public PinnedSectionDecoration(Context context, DecorationCallback decorationCallback) {        Resources res = context.getResources();        this.callback = decorationCallback;        paint = new Paint();        paint.setColor(res.getColor(R.color.colorAccent));        textPaint = new TextPaint();        textPaint.setTypeface(Typeface.DEFAULT_BOLD);        textPaint.setAntiAlias(true);        textPaint.setTextSize(80);        textPaint.setColor(Color.BLACK);        textPaint.getFontMetrics(fontMetrics);        textPaint.setTextAlign(Paint.Align.LEFT);        fontMetrics = new Paint.FontMetrics();        topGap = res.getDimensionPixelSize(R.dimen.sectioned_top);    }    @Override    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {        super.getItemOffsets(outRect, view, parent, state);        int pos = parent.getChildAdapterPosition(view);        long groupId = callback.getGroupId(pos);        if (groupId < 0) return;        if (pos == 0 || isFirstInGroup(pos)) {            outRect.top = topGap;        } else {            outRect.top = 0;        }    }    @Override    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {        super.onDrawOver(c, parent, state);        int itemCount = state.getItemCount();        int childCount = parent.getChildCount();        int left = parent.getPaddingLeft();        int right = parent.getWidth() - parent.getPaddingRight();        float lineHeight = textPaint.getTextSize() + fontMetrics.descent;        long preGroupId, groupId = -1;        for (int i = 0; i < childCount; i++) {            View view = parent.getChildAt(i);            int position = parent.getChildAdapterPosition(view);            preGroupId = groupId;            groupId = callback.getGroupId(position);            if (groupId < 0 || groupId == preGroupId) continue;            String textLine = callback.getGroupFirstLine(position).toUpperCase();            if (TextUtils.isEmpty(textLine)) continue;            int viewBottom = view.getBottom();            float textY = Math.max(topGap, view.getTop());            if (position + 1 < itemCount) { //下一個和當前不一樣移動當前                long nextGroupId = callback.getGroupId(position + 1);                if (nextGroupId != groupId && viewBottom < textY ) {//組內最后一個view進入了header                    textY = viewBottom;                }            }            c.drawRect(left, textY - topGap, right, textY, paint);            c.drawText(textLine, left, textY, textPaint);        }    }}

圖片圖片12G28資訊網——每日最新資訊28at.com

12G28資訊網——每日最新資訊28at.com

本文鏈接:http://m.www897cc.com/showinfo-26-87958-0.htmlRecyclerView中ItemDecoration的精妙用法,實現自定義分隔線、邊距和背景效果

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 從簡單中窺見高端,徹底搞懂任務可中斷機制與任務插隊機制

下一篇: 總結CSS中各個屬性使用百分比(%)基準值

標簽:
  • 熱門焦點
Top 日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不
日韩网站在线观看| 亚洲国产三级在线| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ入口 | 亚洲人成久久| 999亚洲国产精| 亚洲欧美成人网| 久久久水蜜桃| 欧美日韩高清在线| 国产日韩精品一区二区浪潮av| 极品尤物av久久免费看| 91久久在线| 亚洲欧美日韩在线| 老司机午夜免费精品视频| 欧美色综合网| 精品二区视频| 亚洲一区国产| 欧美成人一区二区三区在线观看| 国产精品美女久久| 亚洲国产一区二区三区高清| 亚洲自拍偷拍一区| 欧美成人免费网| 国产欧美日韩伦理| 亚洲开发第一视频在线播放| 亚洲欧美日韩国产综合在线 | 夜夜嗨av一区二区三区免费区| 小处雏高清一区二区三区| 免费亚洲电影在线观看| 国产精品一区一区三区| 亚洲激情午夜| 欧美在线视频免费播放| 欧美日本韩国| 在线电影欧美日韩一区二区私密| 亚洲一区二区在线| 免费在线播放第一区高清av| 国产精品日韩一区| 亚洲精品久久久久中文字幕欢迎你 | 黄色成人精品网站| 亚洲性线免费观看视频成熟| 欧美mv日韩mv国产网站| 国产亚洲制服色| 亚洲一区综合| 欧美久久成人| 一区精品在线| 欧美在线在线| 国产精品另类一区| 亚洲精品一区在线观看| 久久久亚洲综合| 国产精品一区二区三区免费观看| 亚洲美女黄色片| 麻豆精品视频在线观看视频| 国产亚洲欧美一区二区| 亚洲欧美日韩精品久久久| 欧美日韩综合一区| 亚洲精品国产精品国产自| 久久影院午夜论| 国产亚洲欧美另类中文| 亚洲免费视频一区二区| 欧美三级日韩三级国产三级| 亚洲毛片在线看| 欧美aⅴ99久久黑人专区| 黄色小说综合网站| 久久国产精品99国产精| 国产欧美精品日韩精品| 亚洲在线免费| 国产精品久久久久三级| 一区二区三区视频在线看| 欧美激情综合色| 亚洲激情综合| 免费在线观看日韩欧美| 黄色国产精品| 久久久之久亚州精品露出| 国内精品美女在线观看| 欧美在线www| 国产手机视频一区二区| 亚洲一级一区| 国产精品久久91| 亚洲一区中文| 国产精品亚洲欧美| 午夜视频精品| 国产日本欧美一区二区三区在线| 先锋影音网一区二区| 国产精品一二三视频| 午夜天堂精品久久久久| 国产欧美视频一区二区三区| 欧美一区二区三区四区高清| 国产一区 二区 三区一级| 久久精品av麻豆的观看方式| 国产日韩欧美中文| 久久国产精品久久久| 国产一区久久| 久久在线视频| 亚洲级视频在线观看免费1级| 欧美高清一区二区| 日韩视频免费| 国产精品久久久久99| 午夜精品久久久久影视 | 亚洲成色999久久网站| 免费不卡在线视频| 亚洲日本激情| 国产精品福利在线| 欧美一级在线亚洲天堂| 伊人久久大香线蕉av超碰演员| 美女国产一区| 日韩视频一区二区| 国产精品国产三级国产普通话99| 亚洲一区中文| 国内精品美女av在线播放| 欧美va亚洲va香蕉在线| 一区二区三区四区蜜桃| 国产精品久久精品日日| 欧美一区三区三区高中清蜜桃| 国产婷婷色一区二区三区在线| 久久网站热最新地址| 欧美成人69av| 亚洲卡通欧美制服中文| 国产精品第2页| 久久久久国产精品www| 亚洲国产精品久久久久秋霞影院| 欧美片在线播放| 欧美亚洲午夜视频在线观看| 亚洲电影免费| 欧美视频在线一区二区三区| 欧美一级视频一区二区| **欧美日韩vr在线| 欧美日韩在线精品| 欧美在线综合| 亚洲另类黄色| 国产麻豆综合| 欧美www视频| 亚洲男人的天堂在线| 在线观看三级视频欧美| 欧美午夜性色大片在线观看| 久久人人97超碰精品888| 99视频+国产日韩欧美| 国产婷婷一区二区| 欧美激情五月| 久久av一区| 亚洲精品女av网站| 国产视频自拍一区| 欧美欧美天天天天操| 久久国产一区二区| 一区二区三区高清在线观看| 韩国一区电影| 欧美小视频在线| 狂野欧美性猛交xxxx巴西| 在线亚洲观看| 在线观看欧美亚洲| 国产精品久久久久久久久久久久久久 | 亚洲巨乳在线| 狠狠色狠狠色综合人人| 欧美日韩精品一区视频 | 欧美屁股在线| 久久久久一本一区二区青青蜜月| 一区二区三区四区国产| 亚洲电影免费观看高清| 国产精品自拍三区| 欧美日韩免费观看一区=区三区| 久久久噜噜噜久久| 亚洲欧美久久| 亚洲美女淫视频| 一区二区三区在线免费播放| 国产精品乱码一区二区三区| 欧美精品www| 久久天天躁夜夜躁狠狠躁2022 | 亚洲精品女人| 悠悠资源网亚洲青| 国产亚洲精品成人av久久ww| 欧美亚韩一区| 欧美区国产区| 欧美aa在线视频| 久久精品夜色噜噜亚洲a∨| 亚洲综合色激情五月| 99re6这里只有精品| 亚洲高清视频的网址| 国产中文一区二区| 国产欧美视频一区二区| 国产精品区一区| 欧美午夜美女看片| 欧美日韩一区二| 欧美精品999| 欧美顶级少妇做爰| 免费亚洲婷婷| 久久综合九色99| 久久久亚洲高清| 久久九九99| 久久国产一区二区| 欧美专区在线观看| 欧美一区二区三区四区在线| 亚洲欧美成人在线| 亚洲午夜久久久久久久久电影网| 日韩五码在线| 亚洲精品中文在线| 91久久亚洲| 亚洲国产日韩欧美在线99| 影音欧美亚洲| 有坂深雪在线一区| 影视先锋久久| 在线观看视频欧美| 一区二区视频免费完整版观看| 国外成人在线视频| 黄网站免费久久| 亚洲福利在线看| 亚洲国产综合在线看不卡| 亚洲黑丝在线|