2.activity xml布局:
3.添加自己的Bean实体类
4.自定义Adapter继承BaseQuickAdapter
在实现的方法convert(BaseViewHolder helper, Beans item)中添加数据,示例如下
helper.setText(int id,String str) 添加文本
helper.setImageResource(int id,int resouceId)添加图片
helper.addOnClickListener(int id)添加点击事件
helper.setVisible(int id,boolean isInvisible)设置是否invisible
helper.setGone(int id,boolean isVisiable)true为visible,false为gone
5.activity:
1,初始化数据以及adapter绑定recylerView:
swipeRefreshLayout = view.findViewById(R.id.swiperefreshlayout); recyclerView = view.findViewById(R.id.recylerview); itemList = new ArrayList<>(); adapter = new HisOrderAdapter(R.layout.layout_his_order,itemList,status); recyclerView.setAdapter(adapter); adapter.bindToRecyclerView(recyclerView); adapter.setEnableLoadMore(true); adapter.openLoadAnimation(); adapter.disableLoadMoreIfNotFullPage(); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); adapter.setEmptyView(R.layout.view_nodata);
2,设置adapter加载更多监听事件:
adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { recyclerView.postDelayed(new Runnable() { @Override public void run() { httpLoadMore(); } },1000); } }, recyclerView);
3,设置SwipeRefreshLayout加载数据监听:
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refresh(); } });
4,adapter设置子控件点击监听事件:
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { @Override public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { switch (view.getId()){ case R.id.: break; case R.id.: break; } } });
5.adapter设置自身item点击监听事件:
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { } });
6.注意事项:
SwipeRefreshLayout加载完毕结束动画方法为:
swipeRefreshLayout.setRefreshing(false);
adapter加载完毕结束动画方法为:
adapter.loadMoreComplete(); adapter.loadMoreEnd();
adapter更新数据方法为:
adapter.setNewData(list);
ok,大功告成。
ps:BaseQuickAdapter github地址为:https://github.com/CymChad/BaseRecyclerViewAdapterHelper。强大的不得了,有兴趣的可以去see see。