前言
-
在Android开发中,时间轴的 UI非常常见,如下图:
-
储备知识:
1.自定义view基础
2.RecyclerView的使用
3.自定义RecyclerView.ItemDecoration
具体实现
1.最终效果如下:
2.实现思路
- 使用RecyclerView,自定义RecyclerView.ItemDecoration
- 复习ItemDecoration中getItemOffsets()方法,重写onDraw()方法
- 实现RecyclerView.Adapter,绑定数据
3.详细设计
4.具体实现
- 引入RecyclerView依赖包
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>
- 设置item布局
<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" />