XRecyclerview和Recyclerview相似,但是XRecyclerview自带下拉刷新和上拉加载更多的功能,除了引用不一样,其他的都一样,下面写详细代码:
两种可以使用到一个项目中
首先导入依赖
XRecyclerview的依赖:
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
Recyclerview的依赖:
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
首先在布局当中进行引用:
然后在Activity中初始化:
一共有三种方式,对应三种模式
1.线性流:
rv.setLayoutManager(new LinearLayoutManager(this));2.网格
rv.setLayoutManager(new GridLayoutManager(this,3));3.瀑布流
rv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));然后写适配器,带点击事件的适配器,(点击事件使用的是接口回调)
/** * Created by 31962 on 2017/10/20. */ public class GoodsListAdapter extends RecyclerView.Adapter另外,在Activity中,XRecycler中可以设置加载的监听器:{ private List list; private Context context; private OnIClickListener oniclicklistener; public GoodsListAdapter(List list, Context context) { this.list = list; this.context = context; } public void setOnIclicklistener(OnIClickListener oniclicklistener){ this.oniclicklistener = oniclicklistener; } public interface OnIClickListener{ public void onItemclick(GoodListsBean.DatasBean.GoodsListBean bean); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.goodslistitem, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { MyViewHolder myholder = (MyViewHolder) holder; Uri imageuri = Uri.parse(list.get(position).getGoods_image_url()); myholder.sdv.setImageURI(imageuri); myholder.name.setText(list.get(position).getGoods_name()); myholder.price.setText(list.get(position).getGoods_price()); myholder.sale.setText("销量"+list.get(position).getGoods_salenum()); myholder.ll_goodlist.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(oniclicklistener!=null){ oniclicklistener.onItemclick(list.get(position)); } } }); } @Override public int getItemCount() { return list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private final SimpleDraweeView sdv; private final TextView name; private final TextView price; private final TextView sale; private final LinearLayout ll_goodlist; public MyViewHolder(View itemView) { super(itemView); sdv = itemView.findViewById(R.id.Sdv); name = itemView.findViewById(R.id.tv_name_GoodsActivity); price = itemView.findViewById(R.id.tv_price_GoodsActivity); sale = itemView.findViewById(R.id.tv_sale_GoodsActivity); ll_goodlist = itemView.findViewById(R.id.ll_goodlist); } } }
xRv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { myAdapter.refresh(); getInfo(); xRv.refreshComplete(); } @Override public void onLoadMore() { Toast.makeText(UserActivity.this,"哈哈",Toast.LENGTH_SHORT).show(); xRv.loadMoreComplete(); xRv.setLoadingMoreEnabled(true); xRv.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin); String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0"; HttpUtil.getInstance().doGet(url, MusicBean.class, new OnNetListener() { @Override public void onError(IOException e) { } @Override public void onSuccess(MusicBean musicBean) throws IOException { Listsong_list2 = musicBean.getSong_list(); myAdapter.loadmore(song_list2); } }); } });
有的时候有分页加载的需求,分页加载的适配器:
public class MyAdapter extends RecyclerView.Adapter
private Context context;
private List
private OnItemListener onItemListener;
private static int TYPE1 = 0;
private static int TYPE2 = 1;
public interface OnItemListener {
public void onItemClick(ItemBean itemBean, int position);
public void onItemLongClick(ItemBean itemBean, int position);
}
public void setOnItemListener(OnItemListener onItemListener) {
this.onItemListener = onItemListener;
}
public MyAdapter(Context context, List
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载写好的item布局
RecyclerView.ViewHolder viewHolder = null;
if (viewType == TYPE1) {
View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
viewHolder = new Type1Viewholder(view1);
} else {
View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
viewHolder = new Type2Viewholder(view2);
}
return viewHolder;
}
@Override
public int getItemViewType(int position) {
return position % 2 == 0 ? TYPE1 : TYPE2;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//给item布局里的组件设置值
if (holder instanceof Type1Viewholder) {
final ItemBean itemBean = list.get(position);
Type1Viewholder type1Viewholder = (Type1Viewholder) holder;
type1Viewholder.tvName.setText(itemBean.getName());
type1Viewholder.tvAge.setText(itemBean.getAge());
type1Viewholder.ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemListener != null) {
onItemListener.onItemClick(itemBean, position);
}
}
});
type1Viewholder.ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (onItemListener != null) {
onItemListener.onItemLongClick(itemBean, position);
}
return true;
}
});
} else {
Type2Viewholder type2Viewholder = (Type2Viewholder) holder;
}
}
@Override
public int getItemCount() {
return list.size();
}
private class Type1Viewholder extends RecyclerView.ViewHolder {
//类似listview的adapter的viewholder
private TextView tvName;
private TextView tvAge;
private LinearLayout ll;
public Type1Viewholder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvAge = itemView.findViewById(R.id.tv_age);
ll = itemView.findViewById(R.id.ll);
}
}
private class Type2Viewholder extends RecyclerView.ViewHolder {
public Type2Viewholder(View itemView) {
super(itemView);
}
}
public void refresh(List
list.clear();
list.addAll(l);
notifyDataSetChanged();
}
public void addItem(ItemBean itemBean) {
list.add(0, itemBean);
notifyItemInserted(0);
}
可以添加分隔线:引用篇写的好的博客:
http://blog.csdn.net/pengkv/article/details/50538121