Android中使用itemdecoration實(shí)現(xiàn)時(shí)間線效果
代碼如下:
// 時(shí)間線裝飾器 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的個(gè)數(shù)。 與item的個(gè)數(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)圈為實(shí)心圓 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中間的連接線,第一個(gè)item與最后一個(gè)item的連接線需單獨(dú)處理一下。 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的布局會(huì)在減掉這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);
實(shí)現(xiàn)效果如下:
之前在其他地方學(xué)習(xí)到,還可以通過在item layout中實(shí)現(xiàn)時(shí)間線的效果。
參考:
1、啟艦博客。
到此這篇關(guān)于Android中使用itemdecoration實(shí)現(xiàn)時(shí)間線效果的文章就介紹到這了,更多相關(guān)itemdecoration實(shí)現(xiàn)時(shí)間線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)之DrawerLayout實(shí)現(xiàn)抽屜效果
這篇文章主要介紹了Android開發(fā)之DrawerLayout實(shí)現(xiàn)抽屜效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟
這篇文章主要介紹了Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下2020-12-12Android實(shí)現(xiàn)獲取聯(lián)系人電話號(hào)碼功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)獲取聯(lián)系人電話號(hào)碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問的方法
這篇文章主要介紹了Android調(diào)用系統(tǒng)默認(rèn)瀏覽器訪問的方法的相關(guān)資料,需要的朋友可以參考下2016-03-03Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)圖片及網(wǎng)頁的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01kotlin object關(guān)鍵字單例模式實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了kotlin object關(guān)鍵字單例模式實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android文本視圖TextView實(shí)現(xiàn)聊天室效果
這篇文章主要介紹了Android文本視圖TextView實(shí)現(xiàn)聊天室效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05