Android RecyclerView使用(一)
RecyclerView
一种可以替换掉listview gridview 瀑布流等控件的新视图控件,控制视图回收和复用 ==优化内存可以替换listview
与listview的不同之处是:listview缓存convertview不同的是,Recyclerview缓存的是viewholder.
提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现想要的效果
1、RecyclerView.Adapter 适配器
2、RecyclerView.ItemDecoration 每个item附加的子视图,可用来绘制Divider,设置padding等
3、RecyclerView.ItemAnimator 负责添加、删除数据时的动画效果
4、RecyclerView.ViewHolder 缓存的对象
5、RecyclerView.LayoutManager 布局器,负责Item视图的布局
具体使用1、activity:
private Activity activity; private List<PicData> mpicDatas; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycleview); activity = this; // 获取recycleView 设置布局管理器 设置items 分割线 设置 item 动画 RecyclerView my_recycleview = (RecyclerView) this .findViewById(R.id.my_recycleview); my_recycleview.setLayoutManager(new LinearLayoutManager( getApplicationContext())); my_recycleview.setItemAnimator(new DefaultItemAnimator());// 设置默认的动画 mpicDatas = Tool.getData(); adapter = new MyAdapter(mpicDatas); adapter.setOnMyItemClickListener(new OnMyItemClickListener() { @Override public void OnItemClick(View view, int position) { Toast.makeText(activity, "Click" + mpicDatas.get(position), Toast.LENGTH_SHORT).show(); adapter.removeData(position); } @Override public void OnItemLongClick(View view, int position) { Toast.makeText(activity, "LongClick" + mpicDatas.get(position), Toast.LENGTH_SHORT).show(); adapter.addData(position, mpicDatas.get(position)); } }); my_recycleview.setAdapter(adapter); // 自定义动画有问题 // my_recycleview.setItemAnimator(new MyItemAnimator()); }
2、适配器 要处理点击事件和对应的视图处理:
// 提供 个数 和viewholder 已经绑定数据 onBindViewHolder // 触发事件 和动画效果需要自己灵活处理 public class MyAdapter extends RecyclerView.Adapter<ViewHolder> { List<PicData> mpicdatas; private OnMyItemClickListener mOnItemClickListener; public MyAdapter() { } public MyAdapter(List<PicData> mpicdatas) { this.mpicdatas = mpicdatas; } public MyAdapter(List<PicData> mpicdatas, OnMyItemClickListener mOnItemClickListener) { this.mpicdatas = mpicdatas; this.mOnItemClickListener = mOnItemClickListener; } // 移除数据 public void removeData(int po) { if(po>mpicdatas.size()-1){ return; } mpicdatas.remove(po); notifyItemRemoved(po); } // 指定位置添加数据 public void addData(int po, PicData mPicData) { if(po>mpicdatas.size()){ po=mpicdatas.size(); } if(po<0){ po=0; } mpicdatas.add(po, mPicData); notifyItemInserted(po); } // 获取对象个数 @Override public int getItemCount() { return mpicdatas.size(); } // 具体用于绑定对应数据 @Override public void onBindViewHolder(ViewHolder mViewHolder1, int arg1) { final MyViewHolder mViewHolder = (MyViewHolder) mViewHolder1; PicData mPicData = mpicdatas.get(arg1); // mViewHolder.im.setBackground(background) mViewHolder.tv_des.setText(mPicData.getDes()); mViewHolder.tv_name.setText(mPicData.getName()); // 设置点击事件 if (mOnItemClickListener != null) { // 判断是否设置过点击事件 如果设置过 不再设置 防止重复创建 增加内存开销 if (!mViewHolder.itemView.hasOnClickListeners()) { mViewHolder.itemView .setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int po = mViewHolder.getPosition(); mOnItemClickListener.OnItemClick(v, po); } }); mViewHolder.itemView .setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { int po = mViewHolder.getPosition(); mOnItemClickListener.OnItemLongClick(v, po); return true; } }); } } } // 创建一个可返回的holder 用于回收和复用 @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int arg1) { MyViewHolder holder = new MyViewHolder(LayoutInflater.from( RecycleviewActivity.this).inflate( R.layout.activity_recycleview_item, parent, false)); return holder; } // viewholder 静态内部类 class MyViewHolder extends ViewHolder { private TextView tv_name; private TextView tv_des; private ImageView im; public MyViewHolder(View view) { super(view); tv_name = (TextView) view.findViewById(R.id.tv_title); tv_des = (TextView) view.findViewById(R.id.tv_des); im = (ImageView) view.findViewById(R.id.im_holder); } } // 设置点击事件 public void setOnMyItemClickListener( OnMyItemClickListener mOnItemClickListener) { this.mOnItemClickListener = mOnItemClickListener; } }
3、点击事件的接口
import android.view.View; public interface OnMyItemClickListener { public void OnItemClick(View view, int position); public void OnItemLongClick(View view, int position); }
4、model
public class PicData { private String name; private String des; private String path; public PicData() { } public PicData(String name, String des, String path) { this.des = des; this.name = name; this.path = path; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
5、工具
public class Tool { /** * 获取对应的测试数据 * @return */ public static List<PicData> getData(){ String image[]={ "http://imgsrc.baidu.com/forum/w%3D580/sign=f786dbba8744ebf86d716437e9f8d736/9fe5546034a85edf11571c9a49540923df547580.jpg", "http://upload.wendu.cn/2016/0225/thumb_300_0_1456366681530.jpg", "http://upload.wendu.cn/2016/0225/thumb_300_0_1456386396842.jpg", "http://upload.wendu.cn/2016/0225/thumb_300_0_1456386399211.jpg", "http://upload.wendu.cn/2016/0217/thumb_300_0_1455684747338.jpg", "http://upload.wendu.cn/2016/0203/thumb_300_0_1454499202633.png", "http://upload.wendu.cn/2016/0122/thumb_300_0_1453463650114.jpg", "http://upload.wendu.cn/2016/0123/thumb_213_160_1453513340130.png", "http://upload.wendu.cn/2016/0129/thumb_300_0_1454030278472.jpg" }; List<PicData> mlist=new ArrayList<PicData>(); for(int i=0;i<image.length;i++){ mlist.add(new PicData("第几张"+i, "测试第几张图片 测试数据"+i, image[i])); } return mlist; } }
6、布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <android.support.v7.widget.RecyclerView android:id="@+id/my_recycleview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>