色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Android中使用itemdecoration實現(xiàn)時間線效果

瀏覽:14日期: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的個數(shù)。 與item的個數(shù)不一定相同。 int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); // 避免硬編碼,這里通過代碼獲取在getItemOffsets方法中設(shè)置的寬度 int leftDecoWidth = layoutManager.getLeftDecorationWidth(child); // 根據(jù)position獲取當(dāng)前的數(shù)據(jù),然后根據(jù)數(shù)據(jù)狀態(tài)繪制不同的形狀 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) {// 繪制外圈為空心圓,內(nèi)圈為實心圓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設(shè)置的范圍影響,可以繪制在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設(shè)置到recyclerview上。

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

實現(xiàn)效果如下:

Android中使用itemdecoration實現(xiàn)時間線效果

之前在其他地方學(xué)習(xí)到,還可以通過在item layout中實現(xiàn)時間線的效果。

參考:1、啟艦博客。

到此這篇關(guān)于Android中使用itemdecoration實現(xiàn)時間線效果的文章就介紹到這了,更多相關(guān)itemdecoration實現(xiàn)時間線內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 欧美1 | 国产主播精品福利19禁vip | 免费精品一区二区三区在线观看 | 大陆高清自拍 | 草草视频在线免费观看 | 欧美视频 亚洲视频 | 久久久久久久久久久大尺度免费视频 | 三级毛片在线 | 毛片免费观看的视频在线 | 99久久精品国产9999高清 | 国产亚洲精品aaa大片 | 日韩一级片免费 | 港台三级在线观看 | av毛片免费看 | 玖玖精品视频在线 | 男女在线免费视频 | 九月婷婷亚洲综合在线 | 免费观看欧美一级毛片 | 视频二区欧美 | 国产美女作爱视频 | 黄a网站| 91精品国产手机在线版 | 香港aa三级久久三级 | 一级片中文字幕 | 国外成人在线视频 | 全免费a级毛片免费看不卡 全免费毛片在线播放 | 永久免费精品视频 | 国产欧美日韩一区 | 国产亚洲欧洲精品 | 97视频在线免费播放 | 欧美一级毛片欧美大尺度一级毛片 | 日本乱人伦片中文字幕三区 | 久久久久久久久久免费视频 | www夜色| 色三级大全高清视频在线观看 | 久久综合婷婷香五月 | 亚洲第一视频在线观看 | 亚洲国产精品第一区二区 | 步兵精品手机在线观看 | 18videosex性欧美69超高清 | 一级成人毛片免费观看欧美 |