效果图:
code:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private TimelineAdapter timelineAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
List<TimeLineEntity> timeLineEntities = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TimeLineEntity timeLineEntity = new TimeLineEntity("step" + i, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()), i > 5 ? false : true);
timeLineEntities.add(timeLineEntity);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
timelineAdapter = new TimelineAdapter(timeLineEntities);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setAdapter(timelineAdapter);
}
}
public class TimelineAdapter extends RecyclerView.Adapter<TimelineAdapter.Holder> {
private Context mContext;
private List<TimeLineEntity> lineEntityList;
public TimelineAdapter(List<TimeLineEntity> lineEntityList) {
this.lineEntityList = lineEntityList;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
View view = LayoutInflater.from(mContext).inflate(R.layout.timeline_item, parent, false);
Holder holder = new Holder(view);
return holder;
}
@Override
public void onBindViewHolder(Holder holder, int position) {
TimeLineEntity timeLineEntity = lineEntityList.get(position);
holder.tv_title.setText(timeLineEntity.getTitle());
holder.tv_title.setCompoundDrawablesWithIntrinsicBounds(timeLineEntity.isCompleted() ? mContext.getResources().getDrawable(R.drawable.ic_completed) :
mContext.getResources().getDrawable(R.drawable.ic_uncompleted), null, null, null);
holder.tv_desc.setText(timeLineEntity.getDesc());
holder.top_line.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
holder.bottom_line.setVisibility(position == lineEntityList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
}
@Override
public int getItemCount() {
return lineEntityList.size();
}
class Holder extends RecyclerView.ViewHolder {
TextView tv_title;
TextView tv_desc;
View top_line;
View bottom_line;
public Holder(View itemView) {
super(itemView);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_desc = (TextView) itemView.findViewById(R.id.tv_desc);
top_line = (View) itemView.findViewById(R.id.top_line);
bottom_line = (View) itemView.findViewById(R.id.bottom_line);
}
}
}
public class TimeLineEntity {
private String title;
private String desc;
private boolean isCompleted;
public TimeLineEntity(String title, String desc, boolean isCompleted) {
this.title = title;
this.desc = desc;
this.isCompleted = isCompleted;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean completed) {
isCompleted = completed;
}
}
timeline_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<View
android:id="@+id/top_line"
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:background="@android:color/darker_gray" />
<View
android:id="@+id/bottom_line"
android:layout_width="2dp"
android:layout_height="40dp"
android:layout_below="@+id/top_line"
android:layout_marginLeft="15dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:drawableLeft="@drawable/ic_completed"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:text=" 2018/4/17 15:12"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="desc" />
</LinearLayout>
</RelativeLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>