Android實現快遞時間軸功能

2021-02-14 龍旋
前言

具體實現

1.最終效果如下:


2.實現思路

使用RecyclerView,自定義RecyclerView.ItemDecoration

複習ItemDecoration中getItemOffsets()方法,重寫onDraw()方法

實現RecyclerView.Adapter,綁定數據


3.詳細設計

4.具體實現

dependencies {         api 'com.android.support:recyclerview-v7:28.0.0'}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal" />

</RelativeLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >
<TextView android:id="@+id/item_title" android:text="New Text" android:textSize="15sp" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:layout_marginLeft="30dp" android:textSize="15sp" android:id="@+id/item_text" android:layout_below="@+id/item_title" />
</LinearLayout>

public class MyAdapter extends RecyclerView.Adapter {    private LayoutInflater inflater;    private ArrayList<HashMap<String,Object>> listitem;
public MyAdapter(Context context,ArrayList<HashMap<String, Object>> listitem) { this.inflater = LayoutInflater.from(context); this.listitem = listitem; }
class ViewHolder extends RecyclerView.ViewHolder{ private TextView title,text;
public ViewHolder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.item_title); text = itemView.findViewById(R.id.item_text); }
public TextView getTitle() { return title; }
public TextView getText() { return text; }

}


@NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { return new ViewHolder(inflater.inflate(R.layout.list_cell,null)); }
@Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { ViewHolder vh = (ViewHolder) viewHolder; vh.title.setText((CharSequence) listitem.get(i).get("ItemTitle")); vh.text.setText((CharSequence) listitem.get(i).get("ItemText")); }
@Override public int getItemCount() { return listitem.size(); }}

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private final Paint mPaint; private final Paint mPaint1; private final Paint mPaint2; private int itemView_leftinterval; private int itemView_topintervarl; private int circle_radius; private final Bitmap mIcon;

public DividerItemDecoration(Context context){ mPaint = new Paint(); mPaint.setColor(Color.RED);
mPaint1 = new Paint(); mPaint1.setColor(Color.BLUE); mPaint1.setTextSize(30);
mPaint2 = new Paint(); mPaint2.setColor(Color.BLUE); mPaint2.setTextSize(15);
itemView_leftinterval = 200; itemView_topintervarl = 50;
circle_radius = 10; mIcon = BitmapFactory.decodeResource(context.getResources(),R.mipmap.logo);
}
@Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state);
outRect.set(itemView_leftinterval,itemView_topintervarl,0,0);
}
@Override public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.onDraw(c, parent, state);
int childCount = parent.getChildCount(); for (int i=0;i<childCount;i++){ View view = parent.getChildAt(i);

float centerX = view.getLeft() - itemView_leftinterval/3; float centerY = view.getTop() - itemView_topintervarl+(itemView_topintervarl+view.getHeight()/2); c.drawBitmap(mIcon,centerX-circle_radius,centerY-circle_radius,mPaint);
float upLine_up_x = centerX; float upLine_up_y =view.getTop()-itemView_topintervarl;
float upLine_down_x = centerX; float upLine_down_y = centerY-circle_radius;
c.drawLine(upLine_up_x,upLine_up_y,upLine_down_x,upLine_down_y,mPaint);
float bottomLine_up_x = centerX; float bottom_up_y = centerY + circle_radius;
float bottomLine_bottom_x = centerX; float bottomLine_bottom_y = view.getBottom();
c.drawLine(bottomLine_up_x, bottom_up_y, bottomLine_bottom_x, bottomLine_bottom_y, mPaint);

int index = parent.getChildAdapterPosition(view); float Text_x = view.getLeft()-itemView_leftinterval*5/6; float Text_y = upLine_down_y;

switch (index){ case 0: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; case 1: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; case 2: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; case 3: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; case 4: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; case 5: c.drawText("13:40",Text_x,Text_y,mPaint1); c.drawText("2018.4.03",Text_x+5,Text_y+20,mPaint2); break; default:                        c.drawText("已籤收",Text_x,Text_y,mPaint1);            }        }    }}

public class MainActivity extends AppCompatActivity {
private ArrayList<HashMap<String, Object>> itemlist; private RecyclerView rl;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); }
private void initData() { itemlist = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); HashMap<String, Object> map2 = new HashMap<String, Object>(); HashMap<String, Object> map3 = new HashMap<String, Object>(); HashMap<String, Object> map4 = new HashMap<String, Object>(); HashMap<String, Object> map5 = new HashMap<String, Object>(); HashMap<String, Object> map6 = new HashMap<String, Object>();

map1.put("ItemTitle", "中國廣州公司已發出"); map1.put("ItemText", "發件人:妙卡迪文化公司"); itemlist.add(map1);
map2.put("ItemTitle", "國際順豐已收入"); map2.put("ItemText", "等待中轉"); itemlist.add(map2);
map3.put("ItemTitle", "國際順豐轉件中"); map3.put("ItemText", "下一站中國"); itemlist.add(map3);
map4.put("ItemTitle", "中國順豐已收入"); map4.put("ItemText", "下一站江蘇理工大學"); itemlist.add(map4);
map5.put("ItemTitle", "中國順豐派件中"); map5.put("ItemText", "等待派件"); itemlist.add(map5);
map6.put("ItemTitle", "江蘇理工大學已籤收"); map6.put("ItemText", "收件人:darryrzhong"); itemlist.add(map6);
}
private void initView() { rl = findViewById(R.id.my_recycler_view); LinearLayoutManager manager = new LinearLayoutManager(this); rl.setLayoutManager(manager); rl.setHasFixedSize(true); rl.addItemDecoration(new DividerItemDecoration(this)); MyAdapter adapter = new MyAdapter(this,itemlist); rl.setAdapter(adapter); }}

至此,自定義RecyclerView就實現完成了.

到這裡就結束啦.

相關焦點

  • Android實現導航欄添加消息數目提示功能
    我們只需要基本功能2333.一、解需求思路在 RadioGroup 的 RadioButton 上面直接加小圓點,對於我來說實現有點困難,因為我下面還有文字。搞不好,文字就擠沒了。所有我現在在原有的 RadioGroup 上面加一層覆蓋物,類似於 ui 常常接觸的圖層。
  • Android 自定義View篇(十)實現跑馬燈垂直滾動效果
    前言 最近一直鞏固 Android 自定義 View 相關知識,以前都是閱讀一些理論性的文章,很少抽時間自己去實現一個自定義 View,項目中遇到問題就上 github 上去找效果。其實自定義 View 涉及到很多內容,只有親自動手完成幾個案例,才能對相關知識點有深入了解。
  • Android MotionLayout動畫:續寫ConstraintLayout新篇章
    【劃重點】根標籤MotionScene有一個defaultDuration屬性,表示所有未指定時間的動畫的默認時間,默認為300毫秒。MotionScene根標籤 必須包含Transition標籤,可以有多個Transition標籤。
  • Android 4.0新增Space及GridLayout初談
    有很多不錯的文章(比如有:Android Layout Tricks #1, Flattening The Stack)小結了嵌套布局的缺點,歸納有如下三類:  · 不能同時在X,Y軸方向上進行控制項的對齊。  · 當多層布局嵌套時會有性能問題。  · 不能穩定地支持一些支持自由編輯布局的工具。
  • Android 用戶界面---廣播通知(Toast Notifications)
    下面圖1顯示一個例子是鬧鐘應用的廣播通知,一旦鬧鐘被打開,就會在你設置的提醒時間顯示一個廣播通知。 通知持續表示的時間。這個方法會返回一個合適的被實例化的Toast對象。以下,將向你介紹如何實現這些想法。 給廣播通知定位 標準的廣播通知水平居中顯示在屏幕底部附近,你能夠通過setGravity(int, int, int)方法來改變這個位置。
  • 愛剪輯新版更新 新增創新式時間軸功能
    (原標題:愛剪輯新版更新 新增創新式時間軸功能)
  • 【Sobug漏洞時間】Android APP安全測試入門
    BlueStacks下載地址:http://www.bluestacks.net.cn/Download/安裝的時候會提示安裝」給力助手」,給力助手是輔助操作的,可以安裝電腦上下載的app安裝包到模擬器,也可以卸載已經安裝的,還有很多針對模擬器的設置功能,如圖:BlueStacks安裝之後
  • Android在線下載更新功能實踐
    當然,如果你覺得這個方案還有更好的建議,也歡迎在下方留言。默認app下載                .isForce(true) //是否強制更新,默認false 強制更新情況下用戶不同意更新則不能使用app                .update();實現原理使用很簡單吧,其實實現過程也很簡單,大致分為三步:1、根據初入參數判斷是否需要更新2、下載伺服器上的最新apk(通過DownloadManager下載)
  • Android開發之:Toast和Notification
    【IT168技術】之前我們的文章中曾經介紹Dialog,實際上已經實現了提醒功能,在Android中,還可以通過Toast(提醒)和Notification(通知)來實現提醒功能。和Dialog相比,這種提醒更加友好,並且不會打斷用戶的當前操作。
  • android基礎入門
    Android工程師在這幾年都是非常搶手的,其實Android的學習卻並沒有我們想像中那麼難,我們通過幾天的學習就可以做出一個簡單的Android應用程式了,但是如果要深入了解Android,還是要花點時間在基礎上面
  • 為你的Android實現測試覆蓋率
    為你的Android實現測試覆蓋率
  • Android中如何實現圖文混排
    ImageSpan.ALIGN_BASELINE);//用ImageSpan替換文本ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); txtInfo.setText(ss);txtInfo.setMovementMethod(LinkMovementMethod.getInstance()); //實現文本的滾動
  • android 排列 - CSDN
    在windows下有預覽功能,可以在xml中查看布局的樣式,在linux中無。一、LinearLayout線性布局,這個東西,從外框上可以理解為一個div,他首先是一個一個從上往下羅列在屏幕上。即,以屏幕左上角為(0,0)的坐標軸的x,y值,當向下或向右移動時,坐標值將變大。AbsoluteLayout 沒有頁邊框,允許元素之間互相重疊(儘管不推薦)。我們通常不推薦使用 AbsoluteLayout ,除非你有正當理由要使用它,因為它使界面代碼太過剛性,以至於在不同的設備上可能不能很好地工作。
  • android通過代碼實現的多布局專題及常見問題 - CSDN
    Seekbar常規使用方式通過xml布局方式實現,但是由於我們的是sdk,不能有xml布局,所以SeekBar使用純代碼實現。但是這樣就遇到了很多問題。 首先是SeekBar設置setProgressDrawable問題。
  • Android SVG使用之AnimatedVectorDrawable
    (1) 坐標軸為以(0,0)為中心,X軸水平向右,Y軸水平向下(2) 命令與參數間的空格可以省略M 250 150 M250 150android:alpha 該圖片的透明度屬性<group>設置路徑做動畫的關鍵屬性的android:name 定義group的名字android:rotation 定義該group的路徑旋轉多少度android:pivotX 定義縮放和旋轉該group時候的X參考點。
  • Android黑科技之模擬點擊的價值和實現,投廣告者的福音!
    二.MotionEvent實現模擬點擊1. 實現原理1)獲取被點擊的View。2)模擬點擊事件MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP。2. 實現過程哦,冷落了歷史三位大佬挺久了。有請鬼谷先生得意弟子孫臏!
  • 嵌入式掃描模塊在智能快遞的應用,實現快遞自動收發功能
    快遞物流櫃是一種用於快遞行業的裝備,用在終端配送階段,能夠實現快遞包裹的自動收發和遠程查詢及控制,讓收件人在無法及時收到快遞的情況下,快遞員可以將快遞寄存在某個地方,讓收件者可以更好的接收到快遞,其實目前快遞物流櫃就是和個人手機的相結合
  • 為什麼說五軸數控工具機RTCP功能非常重要!
    但無論哪種形式和方法都有著一個共同的特點,就是在加工過程中刀軸方向始終保持不變,工具機只能通過X、Y、Z三個線性軸的插補來實現刀具在空間直角坐標系中的運動。所以,在面對下面這些產品時,三軸工具機效率低、加工表面質量差甚至無法加工的弊端就暴露出來了。 與三軸數控加工設備相比,五聯動數控工具機有以下優點: 1.
  • 快遞掛都愛選10噸軸!BPW把它變更輕了
    【卡車之家 原創】隨著2016年民營快遞企業5家成功上市,整個快遞企業在經營方面,也明顯從粗放型向集約化增長轉變。買什麼裝備用什麼車型才能幫助企業降本增效,各個快遞汽運部門對車輛也產生了更明確的定製需求。
  • 這些都是Android中不規則形狀View的布局實現!
    <com.yxf.clippathlayout.impl.ClipPathFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/clip_path_frame_layout"    android