标签:adapter line eve def setimage orm imageview name cardview
Android RecycleView is more advanced version of ListView with improved performance and other benifits.Using RecyclerView and CardView together,both lists and grids can be created very easily. Here is the complete information anbout RecycleView and other examples.
First , File -> progect Structure -> app dependicies -> add -> cardView
Second , add Recycle View(com.android.support:v7:25.0.0)
Third , new java class ,named Data.
public class Data{
private int ImageId;
private String description;
public Data(){
}
public Data(int ImageId, String description){
this.ImageId = imageId;
this.decription = description
}
alt+insert-> getter & setter
}
Third , new item_layout.xml with root element android.support.v7.widget.CardView , then add LinearLayout in CardView with vertical orientation
Fourth , add ImageView and Description in the LinearLayout.use default image and text to make the layout clearly for developer to design.
Fifth , Add android.support.v7.widget.RecyclerView in main_activity.xml layout file,named recycle ,and scrollbars = "vertical"
Six , new java Class RecycleViewAdapter
class RecycleViewHolder extends RecycleView.ViewHolder{
public ImageView imageview;
public TextView textView;
public RecycleViewHolder(View itemView){
super(itemView);
imageview = itemView.findviewById(R.id.imageView);
textView = itemView.findviewById(R.id.textView);
}
}
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewHolder>{
private List <Data> listData = new ArrayList<Data>();
public RecyclerViewAdapter(List<Data> ListData){
this.listData = listData;
}
@override
onCreateViewHolder
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item,parent,false);
return new RecyclerViewHolder(itemView);
@override
onBindViewHolder
holder.imageView.setImageResource(listData.get(positon).getImageId());
holder.textView.setText(listData).get(position).getText);
@override
getItemCount
}
Seven rewritet MainActivity ,
Private RecyclerView recyclerView;
private RecycleViewAdapter adapter;
private RecycleView.LayoutManager layoutManager;
privater List<Data> listData = new ArrayList<Data> ();
recycleView.setHasFixedSize(true);
layoutManger = new LinearLayoutManager(this);
recycleView.setLayoutManager(layoutManager)
adapter = new RecycleViewAdapter(listData);
recycleView.setAdapter(adapter);
更加详细的用法,推荐一篇很不错的博文:http://blog.csdn.net/lmj623565791/article/details/45059587
标签:adapter line eve def setimage orm imageview name cardview
原文地址:http://www.cnblogs.com/L1nus/p/7572007.html