pulltorefresh(上拉下拉加载数据) 使用方法!

来源:互联网 发布:注册域名要多少钱 编辑:程序博客网 时间:2024/04/27 19:52

注意:

依赖:compile 'com.github.userswlwork:pull-to-refresh:1.0.0'


xml文件:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.wzq.pulltorefreshdemo.MainActivity">


    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />


</android.support.constraint.ConstraintLayout>



主代码:


package com.wzq.pulltorefreshdemo;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


import java.util.ArrayList;
import java.util.List;


/**
 * 接入PullToRefresh
 * 1.引入Library
 * 2.依赖model
 * 3. 布局文件中使用可刷新的控件
 * 4. 设置可刷新监听-->  mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>()
 *      设置可以上拉  mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
 */
public class MainActivity extends AppCompatActivity {
    String baseUrl = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
    private ArrayList<Product.DataBean> list = new ArrayList();
    private PullToRefreshListView plv;
    private ArrayAdapter<String> mAdapter;
    private ArrayAdapter arrayAdapter;
    private  int page = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        plv = (PullToRefreshListView) findViewById( R.id.pull_refresh_list);
        arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list);
        plv.setAdapter(arrayAdapter);
        getNetData(0);


        plv.setMode(PullToRefreshBase.Mode.BOTH);//BOTH 设置支持上下拉
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                // 清空集合,
                list.clear();
                //重新请求数据;更新适配器
                getNetData(0);
            }


            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                //请求数据把请求到的新数据放入大集合;更新适配器
                getNetData(page++);
            }
        });




    }


    private void getNetData(int page) {
        new MAsyncTask().execute(baseUrl+page);


    }


    class MAsyncTask extends AsyncTask<String,Void,String>{




        @Override
        protected String doInBackground(String... strings) {
            return NetWordUtils.getNetjson(strings[0]);
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Product product = gson.fromJson(s, Product.class);
            List<Product.DataBean> listTemp = product.getData();
            list.addAll(listTemp);
            arrayAdapter.notifyDataSetChanged();
            plv.onRefreshComplete();//关闭刷新头和底部;
        }
    }




}

原创粉丝点击