PullToRefresh实现ListView的刷新,加载更多的展示

public class Zi_Xun_Fragment extends Fragment {
    int page_num=1;

    private PullToRefreshListView pullToRefreshListView;
    List list = new ArrayList<>();
    private MyAdapter adapter;
    private ILoadingLayout startLabels;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.zi_xun_frangment_layout, container, false);
        pullToRefreshListView = view.findViewById(R.id.refresh_list_view);
        return view;
        //
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /*设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新");
        startLabels.setRefreshingLabel("正在拉");
        startLabels.setReleaseLabel("放开刷新");
        ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新");
        endLabels.setRefreshingLabel("正在载入...");
        endLabels.setReleaseLabel("放开刷新...");

        getDataFromNet();

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                getRefreshData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                page_num++;
                getaboveData();
            }
        });



    }

    /**
     * 上拉加载更多
     */
    private void getaboveData() {
        String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+page_num;
        GetNetData.getData(getActivity(), path, new JsonStringDataNet() {
            @Override
            public void getJsonData(String json) {
                Gson gson = new Gson();
                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
                list.addAll(dataDataBean.getNewslist());
                setAdapter();
                //停止刷新加载更多
                pullToRefreshListView.onRefreshComplete();

            }
        });
    }

    /**
     * 下拉刷新的方法
     */
    private void getRefreshData() {
        String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1";
        GetNetData.getData(getActivity(), path, new JsonStringDataNet() {
            @Override
            public void getJsonData(String json) {
                list.clear();
                adapter.notifyDataSetChanged();
                Gson gson = new Gson();
                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
                list.addAll(0,dataDataBean.getNewslist());
                setAdapter();
                //停止刷新
                pullToRefreshListView.onRefreshComplete();
                //设置时间
                Date date= new Date(System.currentTimeMillis());
                SimpleDateFormat item = new SimpleDateFormat("HH:mm");
                startLabels.setLastUpdatedLabel(item.format(date));
            }
        });
    }

    //请求网络数据
    private void getDataFromNet() {
        String path = "https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1";

        GetNetData.getData(getActivity(), path, new JsonStringDataNet() {

            //重写的自定义接口中的方法

            @Override
            public void getJsonData(String json) {
            Gson gson = new Gson();
                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
                list.addAll(dataDataBean.getNewslist());
                setAdapter();

            }
        });


    }

    private void setAdapter() {
        //设置适配器
        if (adapter==null) {
            adapter = new MyAdapter(getActivity(), list);
            pullToRefreshListView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }
}

你可能感兴趣的:(安卓开发,移动)