Android时光轴

Android中使用RecyclerView实现时光轴,代码简单易懂.


效果如下:



添加依赖(gradle中):

compile 'com.android.support:recyclerview-v7:23.0.0'




activity中:

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<TimeInfo > list=null;

    @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());
        list=new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add(new TimeInfo());
        }
        TimelineAdapter mAdapter = new TimelineAdapter(this, list);
        recyclerView.setAdapter(mAdapter);
    }
}
adapter中:

public class TimelineAdapter extends  RecyclerView.Adapter<TimelineAdapter.ViewHolder> {

    private static final int ALPHA = 100;

    private List<TimeInfo> list=null;

    private Context context;
    public TimelineAdapter(Context context,List<TimeInfo> list) {
        this.list=list;
        this.context=context;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.v7_item_timeline, null);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText("2016-08-10\n10:20");
        int color = context.getResources().getColor(R.color.colorAccent);
        holder.civ.setFillColor(color);
        holder.civ.setBorderColor(ColorUtils.setAlphaComponent(color, ALPHA));
        holder.img.setBackgroundResource(R.mipmap.ic_zhihu_logo);
        holder.item_timeline_view.setBackgroundResource(list.size()%2==0?R.color.colorAccent:R.color.colorPrimary);
    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        CircleImageView civ;
        ImageView img;
        View item_timeline_view;
        public ViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.item_timeline_time);
            civ= (CircleImageView) v.findViewById(R.id.item_timeline_icon_bg);
            img= (ImageView) v.findViewById(R.id.item_timeline_icon);
            item_timeline_view=v.findViewById(R.id.item_timeline_view);
        }
    }

}

其他的代码见源码.....


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值