Android XRcyclerView刷新,加载更多

来源:互联网 发布:小金鱼摔炮淘宝 编辑:程序博客网 时间:2024/05/17 10:43

效果图:


1.导入依赖:

  /*xrecyclerview*/    compile 'com.jcodecraeer:xrecyclerview:1.3.2'//    imageolader    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'//    logger日志的使用    compile 'com.orhanobut:logger:2.1.1'//    Okhttp请求以及Gson解析    compile 'com.squareup.okio:okio:1.5.0'    compile 'com.squareup.okhttp3:okhttp:3.2.0'    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'    compile 'com.google.code.gson:gson:2.8.2'

2.权限:

 <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

3.布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="xuejian.baway.com.MainActivity">    <com.jcodecraeer.xrecyclerview.XRecyclerView        android:id="@+id/xRecyclerview"        android:layout_width="match_parent"        android:layout_height="match_parent">    </com.jcodecraeer.xrecyclerview.XRecyclerView></RelativeLayout>

4.子条目item.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/img"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"        />    <TextView        android:id="@+id/title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="标题"        /></LinearLayout>

5.MainActivity代码:

public class MainActivity extends AppCompatActivity {    String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=8&gender=2&ts=1871746850&page=";    private XRecyclerView xRecyclerview;    int page=1;    private ArrayList<SuperClass.DataBean> list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        xRecyclerview = (XRecyclerView) findViewById(R.id.xRecyclerview);        //允许刷新,加载更多        xRecyclerview.setPullRefreshEnabled(true);        xRecyclerview.setLoadingMoreEnabled(true);        list = new ArrayList<>();        initView();        //设置布局管理器        xRecyclerview.setLayoutManager(new LinearLayoutManager(this));    }    private void initView() {        xRecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {            @Override            public void onRefresh() {                Toast.makeText(MainActivity.this,"刷新...."+page,Toast.LENGTH_SHORT).show();                list.clear();                page=1;                getData(path,1);                xRecyclerview.refreshComplete();            }            @Override            public void onLoadMore() {                page++;                Toast.makeText(MainActivity.this,"加载更多....第几页"+page,Toast.LENGTH_SHORT).show();                getData(path,page);                xRecyclerview.loadMoreComplete();            }        });        if (page==1){            getData(path,page);        }    }    public void getData(String m,int p) {        OkHttp3Utils.getInstance().doGet(m+p, new GsonObjectCallback<SuperClass>() {            private List<SuperClass.DataBean> data;            //ui操作            @Override            public void onUi(SuperClass superClass) {                data = superClass.getData();                //将请求到的数据添加到集合                list.addAll(data);                Toast.makeText(MainActivity.this,"请求到了"+list.size(),Toast.LENGTH_SHORT).show();                MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);                if (myAdapter!=null){                    xRecyclerview.setAdapter(myAdapter);                }else{                    Toast.makeText(MainActivity.this,"空的"+list.size(),Toast.LENGTH_SHORT).show();                }            }            //失败            @Override            public void onFailed(Call call, IOException e) {            }        });    }}

6.适配器:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{    Context mcontext;    ArrayList<SuperClass.DataBean>  mlist;    public MyAdapter(Context mcontext, ArrayList<SuperClass.DataBean> mlist) {        this.mcontext = mcontext;        this.mlist = mlist;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);        MyViewHolder myViewHolder = new MyViewHolder(view);        return myViewHolder;    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {        ((MyViewHolder)holder).title.setText(mlist.get(position).getTitle());        ImageLoader.getInstance().displayImage(mlist.get(position).getImg(), ((MyViewHolder)holder).img);    }    @Override    public int getItemCount() {        return mlist.size();    }    class MyViewHolder extends RecyclerView.ViewHolder{        public ImageView img;        public TextView title;        public MyViewHolder(View itemView) {            super(itemView);            img = (ImageView) itemView.findViewById(R.id.img);            title = (TextView) itemView.findViewById(R.id.title);        }    }}

7.此处用到的ImageLoader自己建个MyApp:

不要忘了在清单文件里边注册myapp;

public class MyApp extends Application{        public static MyApp mInstance;        @Override        public void onCreate() {            super.onCreate();            mInstance=this;//初始化imageLoader            ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(getApplicationContext());            ImageLoader.getInstance().init(configuration);        }        public static MyApp getInstance() {            return mInstance;        }}
 ps:此处用到的okhttp3utils工具类:关于XRecyclerview可以看这里:http://www.jcodecraeer.com/a/opensource/2015/1126/3723.html