在学习完简单的recycleview后,我发现它还可以添加删除,特意在自己的工程中进行了实现
首先,recycleview控件存在与v7包中,我们在写代码前要导入依赖包:
compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:recyclerview-v7:25.0.0'
再看一下它的重要代码:
del = (Button) findViewById(R.id.delitem); add = (Button) findViewById(R.id.additem); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); //列表布局, 第一个参数表示上下文环境, 第二个参数表示水平布局,第三个参数表示是否反转;后两个参数可省略 //mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)); //表格布局,第一个参数表示上下文,第二个参数表示表格有多少列 //mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 3)); //瀑布布局, mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL)); isflow = true; //添加分割线;列表布局设置DividerItemDecoration.HORIZONTAL;表格布局时设置DividerItemDecoration.VERTICAL; //mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); //添加适配器 mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); 之后,我们就要开始动态添加删除了 这用到了recycleview 的监听时间和一个接口的回调/** * 创建一个回调接口 */ public interface MyItemClickListener { void onItemClick(View view, int position); }之后就是适配器的俄不同了class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> { MyItemClickListener mItemClickListener; @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_home, parent, false); //将全局的监听传递给holder MyViewHolder holder = new MyViewHolder(view, mItemClickListener); return holder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { //控件赋值 holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } // 删除数据 public void removeData(int position) { mDatas.remove(position); //删除动画 notifyItemRemoved(position); notifyDataSetChanged(); } // 添加数据 public void addData(int position) { // 在list中添加数据,并通知条目加入一条 mDatas.add(position, "我是商品" + position); //添加动画 notifyItemInserted(position); }class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private MyItemClickListener mListener; TextView tv; public MyViewHolder(View view, MyItemClickListener myItemClickListener) { super(view); tv = (TextView) view.findViewById(R.id.id_num); 这是对瀑布流时随机高度的一个生成,为了判断是否为瀑布流特地设置了布尔值isflow; if (isflow) { ViewGroup.LayoutParams lp = tv.getLayoutParams(); lp.height = (int) (Math.random() * 300 + 100); tv.setLayoutParams(lp); isflow = false; } //将全局的监听赋值给接口 this.mListener = myItemClickListener; itemView.setOnClickListener(this); } @Override public void onClick(View view) { if (mListener != null) { mListener.onItemClick(view, getPosition()); } } } public void setItemClickListener(MyItemClickListener myItemClickListener) { this.mItemClickListener = myItemClickListener; } } 当然除了接口回调,下面这俩个方法也很重要效果如下图// 删除数据 public void removeData(int position) { mDatas.remove(position); //删除动画 notifyItemRemoved(position); notifyDataSetChanged(); } // 添加数据 public void addData(int position) { // 在list中添加数据,并通知条目加入一条 mDatas.add(position, "我是商品" + position); //添加动画 notifyItemInserted(position); }