RecycleView作为一种可以替代ListView的控件,具有灵活易用等等特性,今天就说一下Recycleview的使用
<android.support.v7.widget.RecyclerView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</android.support.v7.widget.RecyclerView>
创建RecycleView
需要我们自己写一个Adapter这也是重点
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView= LayoutInflater.from(parent.getContext()).
inflate(R.layout.card_main_item, parent, false);
MyViewHolder vh = new MyViewHolder(mView);
return vh;
}
首先是找到Item
LayoutInflater的用法http://blog.csdn.net/lovexieyuan520/article/details/9036673
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String bean = mData.get(position);
holder.tv.setText(bean);
}
@Override
public int getItemCount() {
return mData.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
// public ImageView iv;
public TextView tv;
public MyViewHolder(View arg0) {
super(arg0);
tv = (TextView)arg0.findViewById(R.id.id_num);
}
}