通过对作者时间轴样式以及刚哥博客的学习,在自己撸完代码以后,特此将学习心得记录.
1、app中的build.gradle的中添加依赖:
implementation 'com.orient:Orient-Ui:1.0.0'
添加上此依赖以后,个人以为不需要添加model:Orient-Ui(如果不需要PlaceHolderActivity)(源码中的model),作者已经将功能封装到:com.orient:Orient-Ui:1.0.0
2、因为时间轴样式是基于RecyclerView实现的,因此实现的之前首先实现基础的RecyclerView样式
3、根据自己的需求选择合适的LayoutManager(一般为LinearLayoutManager、GridLayoutManager、DoubleSideLayoutManager),其中DoubleSideLayoutManager为作者自定义的LayoutManager
4、添加数据
5、添加自己的时间轴样式(主要的地方在这里)
选择作者定义好的时间轴类型及设置各种属性(个人感觉像是通用RecyclerView中的横线的设置,将其替换成自定义的时间轴view)
另:个人感觉作者的通用RecyclerAdapter封装的比较好,在此贴出:
package com.lk.timeasixtest.ui.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.lk.timeasixtest.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* @author lk
* @project TimeAsixTest
* @package_name com.lk.timeasixtest.adapter
* @date 2019/10/18
* 描述:基础的通用的RecyclerAdapter
*/
@SuppressWarnings("ALL")
public abstract class RecyclerAdapter<Data> extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder<Data>>
implements View.OnClickListener,View.OnLongClickListener {
// 数据集合
private List<Data> mDataList;
// 监听器
private AdapterListener<Data> adapterListener;
public RecyclerAdapter() {
this(null);
}
public RecyclerAdapter(AdapterListener<Data> adapterListener) {
this(new ArrayList<Data>(),adapterListener);
}
public RecyclerAdapter(List<Data> mDataList, AdapterListener<Data> adapterListener) {
this.mDataList = mDataList;
this.adapterListener = adapterListener;
}
@Override
public ViewHolder<Data> onCreateViewHolder(ViewGroup parent, int viewType) {
// 创建ViewHolder
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View root = inflater.inflate(viewType,parent,false);
ViewHolder<Data> viewHolder = onCreateViewHolder(root,viewType);
// 基础的操作
root.setTag(R.id.recycler_view_holder,viewHolder);
root.setOnClickListener(this);
root.setOnLongClickListener(this);
viewHolder.unbinder = ButterKnife.bind(viewHolder,root);
return viewHolder;
}
/**
* 实际的创建ViewHolder的方法
*/
public abstract ViewHolder<Data> onCreateViewHolder(View root, int viewType);
@Override
public void onBindViewHolder(ViewHolder<Data> holder, int position) {
// 设置不能进行重复绘制
//holder.setIsRecyclable(false);
// TODO 对多数据进行测试,查看哪里出了问题
// 绑定数据
Data data = mDataList.get(position);
holder.bind(data);
}
@Override
public int getItemViewType(int position) {
Data data = mDataList.get(position);
return getItemLayout(data,position);
}
/**
* 得到子布局的ID 适合多种子布局的情况下使用
*/
public abstract int getItemLayout(Data data,int position);
@Override
public int getItemCount() {
return mDataList.size();
}
@Override
public void onClick(View v) {
ViewHolder<Data> holder = (ViewHolder<Data>) v.getTag(R.id.recycler_view_holder);
if(holder != null){
if(adapterListener == null){
return;
}
int pos = holder.getAdapterPosition();
adapterListener.onItemClick(holder,mDataList.get(pos));
}
}
@Override
public boolean onLongClick(View v) {
ViewHolder<Data> holder = (ViewHolder<Data>) v.getTag(R.id.recycler_view_holder);
if(holder != null){
if(adapterListener != null){
int pos = holder.getAdapterPosition();
adapterListener.onItemLongClick(holder,mDataList.get(pos));
return true;
}
}
return false;
}
/**
* 得到数据
*/
public List<Data> getItems() {
return mDataList;
}
/**
* 新增一个数据
*/
public void add(Data data){
mDataList.add(data);
notifyItemInserted(mDataList.size() -1 );
}
/**
* 新增所有的数据
*/
public void addAllData(Collection<Data> datas){
int start = mDataList.size();
mDataList.addAll(datas);
notifyItemRangeChanged(start,mDataList.size()-1);
}
/**
* 新增所有的数组数据
*/
public void addAllData(Data... datas){
int start = mDataList.size();
mDataList.addAll(Arrays.asList(datas));
notifyItemRangeChanged(start,mDataList.size()-1);
}
/**
* 删除所有的数据
*/
public void remove(){
mDataList.clear();
notifyDataSetChanged();
}
/**
* 替换数据
*/
public void replace(Collection<Data> datas){
mDataList.clear();
mDataList.addAll(datas);
notifyDataSetChanged();
}
public void setAdapterListener(AdapterListener<Data> listener){
this.adapterListener = listener;
}
/*
* 适配器的监听器
*/
public interface AdapterListener<Data>{
// 单击的时候
void onItemClick(ViewHolder<Data> holder, Data data);
// 长按的时候
void onItemLongClick(ViewHolder<Data> holder, Data data);
}
/**
* 自定义的ViewHolder
*/
public static abstract class ViewHolder<Data> extends RecyclerView.ViewHolder{
public Unbinder unbinder;
protected Data mData;
public ViewHolder(View itemView) {
super(itemView);
}
public void bind(Data data){
mData = data;
onBind(data);
}
/**
* 实现数据的绑定
*/
protected abstract void onBind(Data data);
}
public abstract static class AdapterListenerImpl<Data> implements AdapterListener<Data>{
@Override
public void onItemClick(ViewHolder<Data> holder,Data data) {
}
@Override
public void onItemLongClick(ViewHolder<Data> holder,Data data) {
}
}
}
使用方式:
在需要使用的Activity或者Fragment中,定义、new对象、实现Adapter中需要重写的方法、添加相应的item、实现需要的监听方法,如:
private RecyclerAdapter<HomePageItem> mAdapter;
mRecyclerView.setAdapter(mAdapter = new RecyclerAdapter<HomePageItem>(createItem(),null) {
@Override
public ViewHolder<HomePageItem> onCreateViewHolder(View root, int viewType) {
return new HomePageActivity.ViewHolder(root);
}
@Override
public int getItemLayout(HomePageItem homePageItem, int position) {
return R.layout.home_page_recycle_item;
}
});
mAdapter.setAdapterListener(new RecyclerAdapter.AdapterListenerImpl<HomePageItem>() {
@Override
public void onItemClick(RecyclerAdapter.ViewHolder<HomePageItem> holder, HomePageItem homePageItem) {
super.onItemClick(holder, homePageItem);
switch (homePageItem.name){
case "两侧布局":
WeekPlanDTLActivity.show(HomePageActivity.this);
break;
case "网格首页":
GridPageActivity.show(HomePageActivity.this);
break;
case "时间轴":
NoteInfoActivity.show(HomePageActivity.this);
break;
case "时间轴两侧布局":
MainActivity.show(HomePageActivity.this);
break;
case "社交媒体(类似于朋友圈首页的布局)":
SocialMediaInfoActivity.show(HomePageActivity.this);
break;
case "活动任务布局":
StepSTLActivity.show(HomePageActivity.this);
break;
case "一周计划两侧布局":
WeekPlanDTLActivity.show(HomePageActivity.this);
break;
case "一周计划(左侧时间,右侧详情)":
WeekPlanSTLActivity.show(HomePageActivity.this);
break;
default:
}
}
});
6、个人发现源码中有一处问题:
在 NoteInfoSTL 类中获取月份的时候:
String mon = calendar.get(Calendar.MONTH)<9?"0"+(calendar.get(Calendar.MONTH)+1)
:Integer.toString(calendar.get(Calendar.MONTH+1));
应该为:
String mon = calendar.get(Calendar.MONTH)<9?"0"+(calendar.get(Calendar.MONTH)+1)
:Integer.toString(calendar.get(Calendar.MONTH)+1);