Android之RecyclerView实现时光轴效果示例
程序员文章站
2023-12-18 11:15:46
做项目的过程中有个需求需要时光轴,于是网上找了部分资料 ,写了个案例,现在分享给大家。
如图:
...
做项目的过程中有个需求需要时光轴,于是网上找了部分资料 ,写了个案例,现在分享给大家。
如图:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 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/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:overscrollmode="never" /> </relativelayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="8dp" android:paddingright="8dp" android:paddingtop="8dp"> <textview android:id="@+id/item_timeline_time" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginright="8dp" android:layout_margintop="18dp" android:gravity="center_horizontal" android:padding="4dp" android:textcolor="@color/coloraccent" android:textsize="16sp" android:text="2015-06-08\n09:56" /> <relativelayout android:id="@+id/item_timeline_icon_layout" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginbottom="4dp" android:layout_marginright="8dp" android:layout_torightof="@id/item_timeline_time"> <com.timelinedemo.circleimageview android:id="@+id/item_timeline_icon_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@android:color/transparent" app:civ_border_width="4dp" /> <imageview android:id="@+id/item_timeline_icon" android:layout_width="24dp" android:layout_height="24dp" android:layout_centerinparent="true" android:scaletype="fitcenter" /> </relativelayout> <textview android:id="@+id/item_timeline_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="18dp" android:layout_torightof="@id/item_timeline_icon_layout" android:text="今日收入" android:textcolor="@color/colorprimary" android:textsize="15sp" /> <textview android:id="@+id/item_timeline_money" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/item_timeline_content" android:layout_margintop="8dp" android:layout_torightof="@id/item_timeline_icon_layout" android:text="$ 100" android:textcolor="@color/colorprimary" android:textsize="22sp" /> <view android:id="@+id/item_timeline_view" android:layout_width="2dp" android:layout_height="60dp" android:layout_alignleft="@id/item_timeline_icon_layout" android:layout_below="@id/item_timeline_icon_layout" android:layout_marginleft="23dp" android:background="@color/coloraccent" /> </relativelayout>
activity.java
public class mainactivity extends appcompatactivity { private recyclerview recyclerview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initlayout(); } private void initlayout(){ recyclerview= (recyclerview) findviewbyid(r.id.recyclerview); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); recyclerview.sethasfixedsize(true); recyclerview.setitemanimator(new defaultitemanimator()); initdata(); } private void initdata(){ list<timeinfo > list=new arraylist<>(); for(int i=0;i<15;i++){ list.add(new timeinfo()); } timelineadapter madapter = new timelineadapter(this, list); recyclerview.setadapter(madapter); } }
添加依赖库:
compile 'com.android.support:recyclerview-v7:23.0.0'
由于代码太多,完整代码未给出,源码直接下载即可
源码点击下载:timelinedemo_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。