RecyclerView的简单使用。
- 远程依赖 compile'com.android.support:recyclerview-v7:25.1.1'
使用新一点的版本Bug少一些,以前的版本会出现测量高度的错误。
1.新建一个自定义adapter
- 按照我的习惯,要实现这样的结构,第一步是写Adapter。
public class SimpleAdapter1 extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
/**
* 创建ViewHolder
*
* @param parent 容器
* @param viewType Item的类型,当RecyclerView只有一种Item时,这个值不用管
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
return null;
}
/**
* 绑定Item的数据
*
* @param holder 对应的ViewHolder
* @param position 下标
*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
}
/**
* @return 反回Adapter的数据长度
*/
@Override
public int getItemCount()
{
return 0;
}
}
RecyclerView自带复用机制 RecyclerView.ViewHolder
当自定义adapter继承 RecyclerView.Adapter,就会重写至少三个方法然后在自定义Adapter内部新建一个内部类ViewHolderItem 继承 RecyclerView.ViewHolder
/**
* HolderItem
*/
private class ViewHolderItem extends RecyclerView.ViewHolder
{
//图片
private ImageView image_head;
//商品名
private TextView tv_name;
//价格
private TextView tv_price;
public ViewHolderItem(View itemView)
{
super(itemView);
image_head = (ImageView) itemView.findViewById(R.id.image_head);
tv_name = (TextView) itemView.findViewById(R.id.tv_name);
tv_price = (TextView) itemView.findViewById(R.id.tv_price);
}
}
- 这里写Adapter的构造方法
private List<BeanProduct> productList = new ArrayList<>();
private Activity activity;
public SimpleAdapter(Activity activity)
{
this.activity = activity;
}
- 创建ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
return new ViewHolderItem(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_product, parent, false));
}
- 绑定数据
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)
{
//给控件设置数据
if (holder instanceof ViewHolderItem)
{
final BeanProduct product = productList.get(position);
//加载图片
Glide.with(activity)
.load(product.getProduct_image())
.into(((ViewHolderItem) holder).image_head);
((ViewHolderItem) holder).tv_name.setText(product.getProduct_name());
((ViewHolderItem) holder).tv_price.setText(product.getProduct_price());
}
}
- 返回数据长度
@Override
public int getItemCount()
{
//返回数据的数量
return productList.size();
}
- 抛出点击事件的接口(按需求来)
/**
* item点击和长按事件接口
*/
public interface OnItemClickListener
{
//点击事件
void onItemClick(View view, int position, BeanProduct product);
//长按事件
void onItemLongClick(View view, int position, BeanProduct product);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
{
this.onItemClickListener = onItemClickListener;
}
- 点击事件接口我一般会写在绑定数据里面
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)
{
//给控件设置数据
if (holder instanceof ViewHolderItem)
{
//...之前写的绑定数据
//抛出点击事件
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition(), product);
}
});
//长按事件
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
onItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition(), product);
return true;
}
});
}
}
- 给个完整的Adapter
public class SimpleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private List<BeanProduct> productList = new ArrayList<>();
private Activity activity;
private OnItemClickListener onItemClickListener;
public SimpleAdapter(Activity activity)
{
this.activity = activity;
}
public SimpleAdapter(Activity activity, List<BeanProduct> productList)
{
this.activity = activity;
addData(productList);
}
/**
* 创建ViewHolder
*
* @param parent 容器
* @param viewType Item的类型,当RecyclerView只有一种Item时,这个值不用管
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
return new ViewHolderItem(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_product, parent, false));
}
/**
* 绑定Item的数据
*
* @param holder 对应的ViewHolder
* @param position 下标
*/
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position)
{
//给控件设置数据
if (holder instanceof ViewHolderItem)
{
final BeanProduct product = productList.get(position);
//加载图片
Glide.with(activity)
.load(product.getProduct_image())
.into(((ViewHolderItem) holder).image_head);
((ViewHolderItem) holder).tv_name.setText(product.getProduct_name());
((ViewHolderItem) holder).tv_price.setText(product.getProduct_price());
//抛出点击事件
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition(), product);
}
});
//长按事件
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
onItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition(), product);
return true;
}
});
}
}
/**
* @return 反回Adapter的数据长度
*/
@Override
public int getItemCount()
{
//返回数据的数量
return productList.size();
}
/**
* HolderItem
*/
private class ViewHolderItem extends RecyclerView.ViewHolder
{
//图片
private ImageView image_head;
//商品名
private TextView tv_name;
//价格
private TextView tv_price;
public ViewHolderItem(View itemView)
{
super(itemView);
image_head = (ImageView) itemView.findViewById(R.id.image_head);
tv_name = (TextView) itemView.findViewById(R.id.tv_name);
tv_price = (TextView) itemView.findViewById(R.id.tv_price);
}
}
/**
* 批量添加数据
*/
public void addData(List<BeanProduct> datas)
{
if (datas != null && datas.size() > 0)
{
this.productList.addAll(datas);
//刷新RecyclerView
notifyDataSetChanged();
}
}
/**
* 添加单个数据
*/
public void insertData(int position, BeanProduct product)
{
if (product != null)
{
this.productList.add(position, product);
notifyItemInserted(position);
}
}
/**
* 移除全部数据
*/
public void clearData()
{
this.productList.clear();
notifyDataSetChanged();
}
/**
* 移除指定数据
*/
public void removeData(int position)
{
if (position < getItemCount())
{
this.productList.remove(position);
notifyItemRemoved(position);
}
}
/**
* item点击和长按事件接口
*/
public interface OnItemClickListener
{
//点击事件
void onItemClick(View view, int position, BeanProduct product);
//长按事件
void onItemLongClick(View view, int position, BeanProduct product);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener)
{
this.onItemClickListener = onItemClickListener;
}
}
2.设置RecyclerView的属性
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//LinearLayoutManager 现行管理器,支持横向、纵向。
//GridLayoutManager 网格布局管理器
//StaggeredGridLayoutManager 瀑布就式布局管理器
//一定要layoutManager,否者无法显示,对应参数:1.上下文,2.方向,3.是否倒序
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
//如果你确定你的RecyclerView每个item大小一样,这样可以提高性能
recyclerView.setHasFixedSize(true);
3.RecyclerView绑定adapter
List<BeanProduct> products = new ArrayList<>();
products.add(new BeanProduct("http://g.hiphotos.baidu.com/zhidao/pic/item/622762d0f703918fee26e819513d269758eec46a.jpg", "路飞", "5亿贝利"));
products.add(new BeanProduct("http://dmimg.5054399.com/allimg/optuji/opgzqz/33.jpg", "索隆", "3亿2千万贝利"));
products.add(new BeanProduct("http://img1.imgtn.bdimg.com/it/u=589352723,993196148&fm=23&gp=0.jpg", "山治", "1亿贝利"));
simpleAdapter = new SimpleAdapter(this, products);
simpleAdapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener()
{
@Override
public void onItemClick(View view, int position, BeanProduct product)
{
//点击事件
Toast.makeText(SimpleRecyclerViewActivity.this
, product.getProduct_name(), Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position, BeanProduct product)
{
//长按事件
Toast.makeText(SimpleRecyclerViewActivity.this
, product.getProduct_price(), Toast.LENGTH_SHORT).show();
}
});
//绑定adapter
recyclerView.setAdapter(simpleAdapter);
4.增删操作
@Override
public void onClick(View v)
{
switch (v.getId())
{
//批量增加数据
case R.id.btn_add:
List<BeanProduct> products = new ArrayList<>();
products.add(new BeanProduct("http://g.hiphotos.baidu.com/zhidao/pic/item/622762d0f703918fee26e819513d269758eec46a.jpg", "路飞", "5亿贝利"));
products.add(new BeanProduct("http://dmimg.5054399.com/allimg/optuji/opgzqz/33.jpg", "索隆", "3亿2千万贝利"));
products.add(new BeanProduct("http://img1.imgtn.bdimg.com/it/u=589352723,993196148&fm=23&gp=0.jpg", "山治", "1亿贝利"));
simpleAdapter.addData(products);
break;
//指定位置插入数据
case R.id.btn_add_insert:
simpleAdapter.insertData(1,new BeanProduct("http://dmimg.5054399.com/allimg/optuji/opgzqz/35.jpg", "罗宾", "1亿2千万贝利"));
break;
//清除数据
case R.id.btn_clear:
simpleAdapter.clearData();
break;
//移除指定下标数据
case R.id.btn_remove:
simpleAdapter.removeData(1);
break;
}
}
其实都是对adapter的操作,然后再刷新数据即可,RecyclerView自带增加删除的动画,自己可以动手试试的。