PullToRefresh实现下拉刷新上拉加载更多

Xml布局



    
    

MathActivity.this

package com.example.kanghuwei.pulltorefresh;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;

public class MainActivity extends BaseActivity {
    String baseUrl = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
    private ArrayListlist = new ArrayList<>();
    private PullToRefreshListView plv;
    private ArrayAdaptermAdapter;
    private int page =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        plv = findViewById(R.id.plv);
        plv.setMode(PullToRefreshBase.Mode.BOTH);

        mAdapter = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1, list);
        plv.setAdapter(mAdapter);

        getNteData(0);

        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
                page = 0;
                list.clear();
                getNteData(page);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
                page++;
                getNteData(page);
            }
        });
    }

    private void getNteData(int page){
        new MAsyncTack().execute(baseUrl+page);
    }

    class MAsyncTack extends AsyncTask{

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

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Gson gson = new Gson();
            ListBean listBean = gson.fromJson(s, ListBean.class);
            List datalist = listBean.getData();
            //空集合
            ArrayList stringlist = new ArrayList<>();

            for (int i= 0; i

BaseActivity

package com.example.kanghuwei.pulltorefresh;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

class BaseActivity extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }
    public void jumpActivity(Class glass){
        Intent intent = new Intent(this,glass);
        startActivity(intent);
    }
}

工具类

package com.example.kanghuwei.pulltorefresh;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetWordUtils {

    private static String  tag = "NetWordUtils";
    
    public static String getNetjson(String urlString) {

        try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");//若果是get请求可以不用配置; 其他请求必须配置
            urlConnection.setConnectTimeout(8000);//设置链接超时间

            InputStream inputStream = urlConnection.getInputStream();//获取网络返回的输入流;

            //可拼接的字符串
            StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp = "";
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuilder.append(temp);
                temp = "";
            }
            //这个是网络获取的数据
            String data = stringBuilder.toString();
            Log.e(tag, "getData: " + data);
            return data;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    
    public static Bitmap getNetBitmap(String urlString) {

        try {
            //用URL封装链接地址;
            URL url = new URL(urlString);
            //用url打开链接
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            //联网的状态码
            int responseCode = urlConnection.getResponseCode();
            if(responseCode ==200){
                //链接上获取输入流
                InputStream inputStream = urlConnection.getInputStream();
                //把流直接转换成bitmap(系统提供的BitmapFactory)
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//BitmapFactory是个工具类,系统提供的
                return bitmap;
            }else {
                Log.e(tag, "网络状态码:: "+responseCode );
            }




        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }


}

你可能感兴趣的:(PullToRefresh实现下拉刷新上拉加载更多)