XRecyclerView的简单使用

请求数据用的是retrofit+Rxjava+MVP框架,这里就不具体写啦!
相信大家都会的哦!下面是XRecyclerView的具体使用:

详解如下:

首先是 MainActivity的布局



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.bwei.lenovo.day0402_xrecylerview.MainActivity">

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/xrecylerview" />

    <ProgressBar
        android:visibility="gone"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/pageress"/>

LinearLayout>



然后是在MainActivity中具体写 XRecyclerView,当然前提是要去请求数据的啦!


package com.bwei.lenovo.day0402_xrecylerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.bwei.lenovo.day0402_xrecylerview.adpter.MyAdpter;
import com.bwei.lenovo.day0402_xrecylerview.model.GoodsBean;
import com.bwei.lenovo.day0402_xrecylerview.model.IModelmpI;
import com.bwei.lenovo.day0402_xrecylerview.presenter.IPresnetrImpI;
import com.bwei.lenovo.day0402_xrecylerview.view.IView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.R.attr.data;

public class MainActivity extends AppCompatActivity implements IView {

    @BindView(R.id.xrecylerview)
    XRecyclerView xrecylerview;
    @BindView(R.id.pageress)
    ProgressBar pageress;

    private int page = 8;
    private boolean flag = false;
    private Map map;
    private IPresnetrImpI iPresnetrImpI;
    private List goodsBeen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        goodsBeen = new ArrayList<>();
        //设置布局管理器
        xrecylerview.setLayoutManager(new LinearLayoutManager(this));

        iPresnetrImpI = new IPresnetrImpI();

        map = new HashMap<>();
        //初始化数据
        initdatas();
    }

    private void initdatas() {
        //允许上拉加载
        xrecylerview.setLoadingMoreEnabled(true);
        //允许下拉刷新
        xrecylerview.setPullRefreshEnabled(true);
        //设置刷新的样式图标
        xrecylerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //设置加载的样式图标
        xrecylerview.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        //设置上拉下拉的监听事件
        xrecylerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            //刷新
            @Override
            public void onRefresh() {

                Log.d("MainActivity", "下拉刷新----------");
                flag = true;
                page++;
                getData();
                xrecylerview.refreshComplete();
            }

            // 加载
            @Override
            public void onLoadMore() {
                Log.d("MainActivity", "上拉加载----------");

                flag = false;
                pageress.setVisibility(View.VISIBLE);
                page++;

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //请求数据的方法
                        getData();
                    }
                }, 2000);
                //关闭上拉的视图
                xrecylerview.loadMoreComplete();
            }
        });
        getData();
    }

    //获取数据
    public void getData() {
        //清空数据
        map.clear();
        //添加参数page
        map.put("page", page + "");
        //调用p层
        iPresnetrImpI.getGoodsList(new IModelmpI(iPresnetrImpI), this);
    }

    //-------------------------重写的---IView方法---------------------------------------
    @Override
    public void showXrecylerView(List list) {
        Log.d("MainActivity", "m层获取到数据---------------" + data);
        pageress.setVisibility(View.GONE);
        //当falg为false
        if (flag) {
            //把list添加到新写的list容器里
            goodsBeen.addAll(list);
            //实例化适配器
            MyAdpter myAdpter = new MyAdpter(MainActivity.this, goodsBeen);
            xrecylerview.setAdapter(myAdpter);
        } else {
            goodsBeen = new ArrayList<>();
            //设置适配器
            MyAdpter myAdpter = new MyAdpter(MainActivity.this, list);
            xrecylerview.setAdapter(myAdpter);
        }
    }
}


你可能感兴趣的:(Android)