根据(https://developer.android.com/intl/zh-cn/training/material/lists-cards.html)简单的学习了使用RecyclerView 和 CardView 来创建列表与卡片后,就想怎么让一个RecyclerView 根据数据呈现不同类型的CardView.
第一步
RecyclerView和Cardview 是Support Library的一部分。所以需要在app/build.gradle中添加以下依赖,才能立即使用
compile 'com.android.support:cardview-v7:24.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:24.0.0-alpha1'
第二步
创建cardview 和RecyclerView
cardviewone.xml
cardviewtwo.xml
main_activity.xml
在MainActivity中设置RecicylerView
RecyclerView recyclerView = (RecyclerView)
findViewById(R.id.recyclerview);RecyclerView.LayoutManager layoutManager = new
LinearLayoutManager(this);recyclerView.setLayoutManager(layoutManager);MyRecyclerAdapter
myRecyclerAdapter =new MyRecyclerAdapter(itemList,this);recyclerView.setAdapter(myRecyclerAdapter);
itemList是一个 ArrayList,Data是一个自定义的类,等会我们会根据这里面的状态值来确定我们哪一个CardView
第三步
自定义一个RecyclerView.Adapter
想要让它显示各种各样的View的话,主要靠的是重载getItemViewType函数
@Override
public int getItemViewType(int position) {
return ItemList.get(position).getType();
}
为每一种View设置一个ViewHolder类
public class OneViewHolder extends CustomViewHolder {
private TextView date; private TextView task;
private Button add;
public OneViewHolder(View itemView) {
super(itemView); date = (TextView)itemView.findViewById(R.id.date);
task = (TextView)itemView.findViewById(R.id.textView);
add = (Button)itemView.findViewById(R.id.button);
}
}
public class TwoViewHolder extends CustomViewHolder {
private TextView date;
private TextView temp;
public TwoViewHolder(View itemView) {
super(itemView);
date = (TextView)itemView.findViewById(R.id.text);
temp = (TextView)itemView.findViewById(R.id.score);
}
}
重载onCreateViewHolder函数
private static final int TASK = 1;
private static final int WEATHER =2;
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(TASK == viewType) {
View v = LayoutInflater.from(mcontext).inflate(R.layout.cardview_one,parent,false);
return new OneViewHolder(v);
} else if(WEATHER == viewType) {
View v = LayoutInflater.from(mcontext).inflate(R.layout.cardview_two,parent,false);
return new TwoViewHolder(v);
}
return null;
}
期中 *viewType 就是之前getItemViewType ***返回的拉
最后一步:onBindViewHolder()绑定数据到正确的Item视图上。
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
int type = ItemList.get(position).type;
switch (type){
case TASK:
OneViewHolder oneViewHolder = (OneViewHolder)holder;
oneViewHolder.date.setText(ItemList.get(position).getData1());
oneViewHolder.task.setText(ItemList.get(position).getData2());
break;
case WEATHER:
TwoViewHolder twoViewHolder = (TwoViewHolder)holder;
twoViewHolder.date.setText(ItemList.get(position).getData1());
twoViewHolder.temp.setText(ItemList.get(position).getData2());
break;
}
}
我自己定义的Data.Class
public class Data{
private int type;
private String data1;
private String data2;
public Data(String data1, String data2, int type) {
this.data1 = data1;
this.data2 = data2;
this.type = type;
}
public String getData1() {
return data1;
}
public String getData2() {
return data2;
}
public int getType() {
return type;
}
}
效果图:虽然丑了一点,但还可以啦