PullToRefreshListView与xlistview的简单使用

PullToRefreshListView使用

package com.bwie.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bwie.adapter.PlvAdapter;
import com.bwie.bean.Bean;
import com.bwie.pulltorefresh_test01.R;
import com.bwie.utils.Utils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

/**
 * Created by zhan on 2018/1/8.
 */

public class Fragment01 extends Fragment {
    private View view;
    private PullToRefreshListView plv;
    String url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
    private int page=0;
    private List<Bean.DataBean> list;
    private PlvAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01, container, false);
        initView(view);
        getNetData();

        plv.setMode(PullToRefreshBase.Mode.BOTH);
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                list.clear();
                getNetData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getNetData();
                adapter.notifyDataSetChanged();
            }
        });
        return view;
    }

    private void getNetData() {
        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {
                String json = new Utils().getJson(url+page);
                return json;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                list = bean.getData();
                adapter = new PlvAdapter(getActivity(), list);
                plv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                plv.onRefreshComplete();//关闭刷新头和底部;
            }
        }.execute();
    }

    private void initView(View view) {
        plv = (PullToRefreshListView) view.findViewById(R.id.plv);
    }
}


 xlistview的使用

package com.bwie.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bawei.wzq.wzlibrary.view.XListView;
import com.bwie.adapter.PlvAdapter;
import com.bwie.bean.Bean;
import com.bwie.utils.Utils;
import com.bwie.pulltorefresh_test01.R;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by zhan on 2018/1/5.
 */

public class Fragment02 extends Fragment implements XListView.IXListViewListener{
    String json_url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=";
    int page=1;
    private View view;
    private XListView xlv;
    private List<Bean.DataBean> data;
    private Bean bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment02, container, false);
        initView(view);
        getData();

        return view;
    }

    private void initView(View view) {
        xlv = (XListView) view.findViewById(R.id.xlv);
        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);

    }

    @Override
    public void onRefresh() {
        data.clear();
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }
    public void getData(){
        new AsyncTask<String, Integer, String>() {

            private PlvAdapter adapter;

            @Override
            protected String doInBackground(String... strings) {
                String jsonstr = new Utils().getJson(json_url+page);
                return jsonstr;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                bean = gson.fromJson(s, Bean.class);
                data = bean.getData();
                adapter = new PlvAdapter(getActivity(), data);
                xlv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                uiComplete();
            }
        }.execute();
    }
    public void  uiComplete(){
        xlv.stopRefresh();
        xlv.stopLoadMore();
        xlv.setRefreshTime("刚刚");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值