国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android中使用itemdecoration實現時間線效果

瀏覽:18日期:2022-09-20 17:28:49

代碼如下:

// 時間線裝飾器public class TimeLineDecoration extends RecyclerView.ItemDecoration { private Paint mPaint; public TimeLineDecoration() { mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(5); } @Override public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.onDraw(c, parent, state); RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); // 這里的childcount為可見item的個數。 與item的個數不一定相同。 int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); // 避免硬編碼,這里通過代碼獲取在getItemOffsets方法中設置的寬度 int leftDecoWidth = layoutManager.getLeftDecorationWidth(child); // 根據position獲取當前的數據,然后根據數據狀態繪制不同的形狀 int position = parent.getChildAdapterPosition(child); int cx = leftDecoWidth / 2; int cy = child.getTop() + child.getHeight() / 2; int radius = 20; if (position == 2) {c.drawRect(cx - radius, cy - radius, cx + radius, cy + radius, mPaint); } else if (position == 4) {// 繪制外圈為空心圓,內圈為實心圓mPaint.setStyle(Paint.Style.STROKE);c.drawCircle(cx, cy, radius, mPaint);mPaint.setStyle(Paint.Style.FILL);c.drawCircle(cx, cy, radius >> 1, mPaint); } else {c.drawCircle(cx, cy, radius, mPaint); } // 繪制item中間的連接線,第一個item與最后一個item的連接線需單獨處理一下。 if (position == 0) {c.drawLine(cx, cy + mPaint.getStrokeWidth() + radius, cx, child.getBottom(), mPaint); } else if (position == parent.getAdapter().getItemCount() - 1) {c.drawLine(cx, child.getTop(), cx, cy - mPaint.getStrokeWidth() - radius, mPaint); } else {c.drawLine(cx, cy + mPaint.getStrokeWidth() + radius, cx, child.getBottom(), mPaint);c.drawLine(cx, child.getTop(), cx, cy - mPaint.getStrokeWidth() - radius, mPaint); } } } @Override public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.onDrawOver(c, parent, state); // 不受outRect設置的范圍影響,可以繪制在item上。 } @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); // 在item左邊留下100像素的空間。 item的布局會在減掉這100像素后處理。 outRect.left = 100; }}

然后將該itemdecoration設置到recyclerview上。

RecyclerAdapter adapter = new RecyclerAdapter(this, data);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.addItemDecoration(new TimeLineDecoration());mRecyclerView.setAdapter(adapter);

實現效果如下:

Android中使用itemdecoration實現時間線效果

之前在其他地方學習到,還可以通過在item layout中實現時間線的效果。

參考:1、啟艦博客。

到此這篇關于Android中使用itemdecoration實現時間線效果的文章就介紹到這了,更多相關itemdecoration實現時間線內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 日本久久久久久久 | 在线精品视频免费观看 | 国产精品九九久久一区hh | 高清不卡日本v在线二区 | 女人张开双腿让男人 | 中文字幕在线播放视频 | 欧美精选在线 | 欧美日韩在线播一区二区三区 | 国产一级生活片 | 一级做a爰性色毛片免费 | 亚洲成a人片毛片在线 | 12一15女人a毛片 | 欧美13一14周岁a在线播放 | 可以免费看黄的网址 | 特级毛片在线播放 | 欧美一级特黄特黄毛片 | 纯欧美一级毛片_免费 | 一区二区三区不卡视频 | 国产码一区二区三区 | 欧美一级片网址 | 欧美午夜不卡 | 久久久久久亚洲精品影院 | 免看一级a毛片一片成人不卡 | 91精品一区二区三区在线播放 | 看国产一级毛片 | 亚洲日本一区二区三区在线 | 萌白酱国产一区 | 欧美性久久久久 | 亚洲天堂免费看 | 久久久久久综合一区中文字幕 | 在线成人| 日本免费观看的视频在线 | 一级做a爰片毛片 | 精品日韩一区二区三区视频 | 亚洲国产精品一区二区第四页 | 国产成人系列 | 国产精品9999久久久久 | 一国产一级淫片a免费播放口 | 国产精品久久一区二区三区 | 加勒比一区在线 | 孕妇交 |