注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

罔少年

QQ 332256483

 
 
 

日志

 
 

用RecyclerView实现时间轴列表  

2017-02-17 08:47:19|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
最近挺流行的,应用在快递,球赛直播,个人账单等,以时间为单位的数据展示中
用RecyclerView实现时间轴列表 - 我是小黄人 - 罔少年
 
具体实现
----------------------------------------------------------------------------------------------------------------------------------------
1、导入RecyclerView库
compile 'com.android.support:recyclerview-v7:25.1.1'
2、activity布局文件activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
/>
</RelativeLayout>
3、item布局文件item_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="32dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_dot"
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/shape_normal_item_main"
android:contentDescription="圆点" />
<TextView
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#999" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2017-01-01 12:00" />
<TextView
android:id="@+id/txt_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="快件在浙江,准备发往下一站,广州" />
</LinearLayout>
</LinearLayout>
4、RecyclerView数据源对象Express.java
public class Express {
/** 时间 */
private String acceptTime;
/** 描述 */
private String acceptStation;
public Express() {
}
public Express(String acceptTime, String acceptStation) {
this.acceptTime = acceptTime;
this.acceptStation = acceptStation;
}
public String getAcceptTime() {
return acceptTime;
}
public void setAcceptTime(String acceptTime) {
this.acceptTime = acceptTime;
}
public String getAcceptStation() {
return acceptStation;
}
public void setAcceptStation(String acceptStation) {
this.acceptStation = acceptStation;
}
}
5、Activity实现,这里把Adapter和HolderView写在一个类了
public class MainActivity extends Activity {
private RecyclerView recyclerView;
private List<Express> list = new ArrayList<Express>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initLayout();
}

private void initLayout() {
recyclerView = (RecyclerView)this.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter(MainActivity.this, list));
}

private void initData() {
list.add(new Express("2016-05-25 17:48:00", "[宁波] [宁波北仑]的派件已签收 感谢使用中通快递"));
list.add(new Express("2016-05-25 14:13:00", "[宁波] [宁波北仑]的宁波大学代理点正在派件 18040xxxxxx 请保持电话畅通"));
list.add(new Express("2016-05-25 13:01:04", "[宁波] 快件到达 [宁波北仑]"));
list.add(new Express("2016-05-25 12:19:47", "[宁波] 快件离开 [宁波中转]已发往[宁波北仑]"));
list.add(new Express("2016-05-25 11:12:44", "[宁波] 快件到达 [宁波中转]"));
list.add(new Express("2016-05-24 03:12:12", "[杭州] 快件离开 [杭州中转部]已发往[宁波中转]"));
list.add(new Express("2016-05-23 21:06:46", "[杭州] 快件到达 [杭州汽运部]"));
list.add(new Express("2016-05-23 18:59:41", "[杭州] 快件离开 [杭州乔司区]已发往[宁波]"));
list.add(new Express("2016-05-23 18:35:32", "[杭州] [杭州乔司区]的市场部已收件 电话:18358xxxxxx"));
}

class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Express> mList = new ArrayList<>();
private LayoutInflater inflater;
public MyAdapter(Context context, List<Express> list) {
inflater = LayoutInflater.from(context);
this.mList = list;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
return new MyViewHolder(inflater.inflate(R.layout.item_miain, viewGroup, false));
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
MyViewHolder myViewHolder = (MyViewHolder)viewHolder;
//如果是第一个item,则颜色加深
if(getItemViewType(position) == 0) {
myViewHolder.iv_dot.setImageResource(R.drawable.shape_first_item_main);
myViewHolder.txt_time.setTextColor(0xff555555);
myViewHolder.txt_content.setTextColor(0xff555555);
}else {
myViewHolder.iv_dot.setImageResource(R.drawable.shape_normal_item_main);
myViewHolder.txt_time.setTextColor(0xff999999);
myViewHolder.txt_content.setTextColor(0xff999999);
}
//显示item内容
myViewHolder.bindHolder(mList.get(position));
}

@Override
public int getItemCount() {
return mList.size();
}

@Override
public int getItemViewType(int position) {
if(position == 0) {
return 0;
}else {
return 1;
}
}
}

class MyViewHolder extends RecyclerView.ViewHolder {
//圆点
private ImageView iv_dot;
//时间
private TextView txt_time;
//内容
private TextView txt_content;

public MyViewHolder(View itemView) {
super(itemView);
iv_dot = (ImageView) itemView.findViewById(R.id.iv_dot);
txt_time = (TextView) itemView.findViewById(R.id.txt_time);
txt_content = (TextView) itemView.findViewById(R.id.txt_content);
}

//显示item内容
public void bindHolder(Express express) {
txt_time.setText(express.getAcceptTime());
txt_content.setText(express.getAcceptStation());

}
}
}
源码下载:
我是程序猿,文字底部
  评论这张
 
阅读(79)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017