Android實(shí)現(xiàn)列表時(shí)間軸
本文實(shí)例為大家分享了Android列表時(shí)間軸展示的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的效果圖如下:
實(shí)現(xiàn)的方式是利用recycleview的ItemDecoration這個(gè)抽象類,就是我們經(jīng)常用來畫分割線的的這個(gè)類,
具體如下
public class DividerItemDecoration extends RecyclerView.ItemDecoration{ // 寫右邊字的畫筆(具體信息) private Paint mPaint; // 寫左邊日期字的畫筆( 時(shí)間 + 日期) private Paint mPaint1; private Paint mPaint2; private Paint mPaint3; // 左 上偏移長(zhǎng)度 private int itemView_leftinterval; private int itemView_topinterval; // 軸點(diǎn)半徑 private int circle_radius; // 圖標(biāo) private Bitmap mIcon; //月份合集(使用時(shí)需要設(shè)置) private List<String> monthList= new ArrayList<>(); //年份合集(使用時(shí)需要設(shè)置) private List<String> yearList= new ArrayList<>(); public void setMonthList(List<String> monthList) { this.monthList = monthList; } public void setYearList(List<String> yearList) { this.yearList = yearList; } // 在構(gòu)造函數(shù)里進(jìn)行繪制的初始化,如畫筆屬性設(shè)置等 public DividerItemDecoration(Context context) { // 軸點(diǎn)畫筆(紅色) mPaint = new Paint(); mPaint.setColor(Color.rgb(58, 154, 239)); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(3); // 左邊時(shí)間文本畫筆(藍(lán)色) // 此處設(shè)置了兩只分別設(shè)置 時(shí)分 & 年月 mPaint1 = new Paint(); mPaint1.setColor(Color.BLACK); mPaint1.setTextSize(30); mPaint2 = new Paint(); mPaint2.setColor(context.getResources().getColor(R.color.divider)); mPaint2.setTextSize(26); mPaint3 = new Paint(); mPaint3.setColor(Color.rgb(58, 154, 239)); mPaint3.setTextSize(20); // 賦值ItemView的左偏移長(zhǎng) itemView_leftinterval = 150; // 賦值ItemView的上偏移長(zhǎng)度 itemView_topinterval = 30; // 賦值軸點(diǎn)圓的半徑為10 circle_radius = 8; } // 重寫getItemOffsets()方法 // 作用:設(shè)置ItemView 左 & 上偏移長(zhǎng)度 @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); // 設(shè)置ItemView的左 & 上偏移長(zhǎng)度分別為150 px & 30px,即此為onDraw()可繪制的區(qū)域 outRect.set(itemView_leftinterval, itemView_topinterval, 0, 0); } // 重寫onDraw() // 作用:在間隔區(qū)域里繪制時(shí)光軸線 & 時(shí)間文本 @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); // 獲取RecyclerView的Child view的個(gè)數(shù) int childCount = parent.getChildCount(); // 遍歷每個(gè)Item,分別獲取它們的位置信息,然后再繪制對(duì)應(yīng)的分割線 for (int i = 0; i < childCount; i++) { // 獲取每個(gè)Item對(duì)象 View child = parent.getChildAt(i); View lastChild = null; if (i > 0) { lastChild = parent.getChildAt(i - 1); } /** * 繪制軸點(diǎn) */ // 軸點(diǎn) = 圓 = 圓心(x,y) 位置可以根據(jù)需求來調(diào)節(jié) float centerx = child.getLeft() - itemView_leftinterval / 4; float centery = child.getTop() + itemView_topinterval +10; // 繪制軸點(diǎn)圓 c.drawCircle(centerx, centery, circle_radius, mPaint); /** * 繪制上半軸線(x軸是保持不變的) */ // 上端點(diǎn)坐標(biāo)(x,y) float upLine_up_x = centerx; float upLine_up_y = 0; if (i>0){ upLine_up_y = lastChild.getBottom(); }else { upLine_up_y = centery - itemView_topinterval; } // 下端點(diǎn)坐標(biāo)(x,y) float upLine_bottom_x = centerx; float upLine_bottom_y = centery - circle_radius; //繪制上半部軸線 c.drawLine(upLine_up_x, upLine_up_y, upLine_bottom_x, upLine_bottom_y, mPaint); /** * 繪制下半軸線,最后一個(gè)不畫下半軸 */ if (i <childCount-1){ // 上端點(diǎn)坐標(biāo)(x,y) float bottomLine_up_x = centerx; float bottom_up_y = centery + circle_radius; // 下端點(diǎn)坐標(biāo)(x,y) float bottomLine_bottom_x = centerx; float bottomLine_bottom_y = child.getBottom(); //繪制下半部軸線 c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, mPaint); } /** * 繪制左邊時(shí)間文本 */ // 獲取每個(gè)Item的位置 int index = parent.getChildAdapterPosition(child); // 設(shè)置文本起始坐標(biāo) float Text_x = child.getLeft() - itemView_leftinterval * 5 / 6; float Text_y = upLine_bottom_y; // 根據(jù)Item位置設(shè)置時(shí)間文本 switch (index) { case (0): // 設(shè)置日期繪制位置 c.drawText(monthList.get(0), Text_x, Text_y, mPaint1); c.drawText(yearList.get(0), Text_x + 8, Text_y + 28, mPaint2); break; case (1): // 設(shè)置日期繪制位置 c.drawText(monthList.get(1), Text_x, Text_y, mPaint1); c.drawText(yearList.get(1), Text_x + 8, Text_y + 28, mPaint2); break; case (2): // 設(shè)置日期繪制位置 if (TextUtils.isEmpty(yearList.get(2))){ c.drawText(monthList.get(2), Text_x, Text_y, mPaint3); }else { c.drawText(monthList.get(2), Text_x, Text_y, mPaint1); c.drawText(yearList.get(2), Text_x + 8, Text_y + 28, mPaint2); } break; case (3): // 設(shè)置日期繪制位置 c.drawText(monthList.get(3), Text_x, Text_y, mPaint1); c.drawText(yearList.get(3), Text_x + 8, Text_y + 28, mPaint2); break; case (4): // 設(shè)置日期繪制位置 c.drawText(monthList.get(4), Text_x, Text_y, mPaint1); c.drawText(yearList.get(4), Text_x + 8, Text_y + 28, mPaint2); break; default:c.drawText("結(jié)束", Text_x, Text_y, mPaint1); } } } }
使用比較簡(jiǎn)單:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this); dividerItemDecoration.setMonthList(monthList); dividerItemDecoration.setYearList(yearList); mRecyclerView.addItemDecoration(dividerItemDecoration);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
- Android recyclerview實(shí)現(xiàn)縱向虛線時(shí)間軸的示例代碼
- Android自定義控件實(shí)現(xiàn)時(shí)間軸
- Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果
- Android自定義view仿淘寶快遞物流信息時(shí)間軸
- Android實(shí)現(xiàn)快遞物流時(shí)間軸效果
- Android自定義指示器時(shí)間軸效果實(shí)例代碼詳解
- 教你3分鐘了解Android 簡(jiǎn)易時(shí)間軸的實(shí)現(xiàn)方法
- Android自定義View實(shí)現(xiàn)垂直時(shí)間軸布局
- android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(1)
相關(guān)文章
Android編程實(shí)現(xiàn)應(yīng)用自動(dòng)更新、下載、安裝的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)應(yīng)用自動(dòng)更新、下載、安裝的方法,涉及Android針對(duì)應(yīng)用程序包的讀取,屬性判斷與更新操作的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路
用戶退出應(yīng)用前給出一個(gè)提示是很有必要的,因?yàn)榭赡苁怯脩舨⒉徽娴南胪顺?,而只是一不小心按下了返回鍵,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個(gè)Dialog提示框;個(gè)人覺得再按一次返回鍵退出程序很有必要,接下來介紹一些簡(jiǎn)單實(shí)現(xiàn)2013-01-01Android移動(dòng)應(yīng)用開發(fā)指南之六種布局詳解
Android應(yīng)用界面要美觀好看,就需要運(yùn)用到一定的布局技術(shù),Android布局是不可忽視的,是android應(yīng)用界面開發(fā)的重要一環(huán),這篇文章主要給大家介紹了關(guān)于Android移動(dòng)應(yīng)用開發(fā)指南之六種布局的相關(guān)資料,需要的朋友可以參考下2022-09-09Android-自定義控件之ListView下拉刷新的實(shí)現(xiàn)
本篇文章主要介紹了Android-自定義控件之ListView下拉刷新示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android實(shí)現(xiàn)知乎選項(xiàng)卡動(dòng)態(tài)隱藏效果實(shí)例
選項(xiàng)卡相信對(duì)大家來說應(yīng)該不陌生,最近發(fā)現(xiàn)知乎選項(xiàng)卡的動(dòng)態(tài)隱藏效果不錯(cuò),下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)知乎選項(xiàng)卡動(dòng)態(tài)隱藏效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02Android 使用FragmentTabHost實(shí)現(xiàn)底部菜單功能
這篇文章主要介紹了Android 使用FragmentTabHost實(shí)現(xiàn)底部菜單功能,詳細(xì)給大家介紹了FragmentTabHost配合Fragment的使用方法,需要的朋友可以參考下2017-12-12Android優(yōu)化之啟動(dòng)頁(yè)去黑屏實(shí)現(xiàn)秒啟動(dòng)
本文的內(nèi)容主要是講Android啟動(dòng)頁(yè)優(yōu)化,去黑屏實(shí)現(xiàn)秒啟動(dòng)的功能,有需要的小伙伴們可以參考學(xué)習(xí)。2016-08-08Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)硪黄狝ndroid設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10android內(nèi)存優(yōu)化之圖片優(yōu)化
對(duì)圖片本身進(jìn)行操作。盡量不要使用setImageBitmap、setImageResource、BitmapFactory.decodeResource來設(shè)置一張大圖,因?yàn)檫@些方法在完成decode后,最終都是通過java層的createBitmap來完成的,需要消耗更多內(nèi)存2012-12-12