XRecyclerView的下拉刷新与加载
XRecyclerView可以实现下拉刷新和上拉加载,是一个比较常用的框架。
使用的时候我们需要先导入依赖,
compile 'com.jcodecraeer:xrecyclerview:1.5.8' implementation 'com.android.support:design:26+'//TabLayout依赖
导入依赖之后,我们可以在布局文件中使用控件。
适配器的写法与RecyclerView的使用方法类似:
public class ProductsXRecyclerAdapter extends XRecyclerView.Adapter<ProductsXRecyclerAdapter.MyViewHolder> { private Activity activity; private List<ProductsBean.DataBean> data; public ProductsXRecyclerAdapter(Activity activity, List<ProductsBean.DataBean> data) { this.activity = activity; this.data = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(activity).inflate(R.layout.products_recycler_item_layout, null); MyViewHolder myViewHolder = new MyViewHolder(inflate); return myViewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(activity, DetailActivity.class); int pid = data.get(position).getPid(); intent.putExtra("pid",pid + ""); activity.startActivity(intent); activity.finish(); } }); String images = data.get(position).getImages(); String[] split = images.split("!"); Glide.with(activity).load(split[0]).into(holder.img); holder.title.setText(data.get(position).getTitle()); holder.price.setText("¥" + data.get(position).getPrice() + ""); holder.price.setText("¥" + data.get(position).getBargainPrice() + ""); } @Override public int getItemCount() { return data.size(); } class MyViewHolder extends XRecyclerView.ViewHolder{ private final ImageView img; private final TextView title; private final TextView price; private final TextView youhuijia; public MyViewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.products_recycler_img); title = itemView.findViewById(R.id.products_recycler_text_title); price = itemView.findViewById(R.id.products_recycler_text_price); youhuijia = itemView.findViewById(R.id.products_recycler_text_youhuijia); } } }
java代码:
private XRecyclerView product_recycler; private List<ProductsBean.DataBean> data = new ArrayList<>(); private ProductsXRecyclerAdapter adapter; private int page = 1; private Map<String,String> map = new HashMap<>(); private int pscid; private IParesenter iParesenter = new ProductsPresenter(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_products); initView();//先加载控件 pscid = getIntent().getIntExtra("pscid",0);//需要的参数 loadData();//默认加载第一页的数据 product_recycler.setLoadingMoreEnabled(true); product_recycler.setLoadingListener(new XRecyclerView.LoadingListener() {//上拉下拉的监听 @Override public void onRefresh() { page = 1; loadData();//下拉刷新时page为1,请求网络 } @Override public void onLoadMore() {//上拉加载更多时,page++,请求网络 page ++; loadData(); } }); } public void loadData(){ if (page > 3){//因为页面最多有3页,如果page>3那么提示信息 Toast.makeText(this, "没有更多了", Toast.LENGTH_SHORT).show(); product_recycler.loadMoreComplete(); }else {//存储参数的集合每次都需要清空之后再传入参数 map.clear(); map.put("pscid",pscid + ""); map.put("page",page + ""); iParesenter.getDatahasParams(map); } } public void initView(){ product_recycler = findViewById(R.id.products_xrecycler); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); product_recycler.setLayoutManager(linearLayoutManager); adapter = new ProductsXRecyclerAdapter(this,data); product_recycler.setAdapter(adapter); } @Override public void getProductsSuccess(ProductsBean productsBean) {//网络请求成功之后的方法 if (page == 1){//如果page==1,那么代表是刷新方法,需要将data集合中的数据清空。 data.clear(); product_recycler.refreshComplete(); }else {//page != 1那么是加载更多 product_recycler.loadMoreComplete(); } data.addAll(productsBean.getData());//将数据添加到集合中,刷新适配器,更新界面 adapter.notifyDataSetChanged(); } @Override public void getProductsFailed(String error) {//获取数据失败的回调,log打印错误信息 Log.d("test",error); }
发布评论
热门评论区: