Fragment的显示页面刷新使用

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
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.xlistview.XListView;
import com.google.gson.Gson;

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



public class ConcernFragment extends Fragment {

    private XListView listView;
    private List list;
    private NewsAdapter adapter;
    private int page =1;
    private Handler handler = new Handler();
    private static String url = "http://www.xieast.com/api/news/news.php?page=";
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //引入布局
        View v = inflater.inflate(R.layout.fragment_concern,container,false);
        listView = v.findViewById(R.id.plv_news);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //封装类的list
        list = new ArrayList<>();
        //Activity的适配器
        adapter = new NewsAdapter(getActivity(),list);
        listView.setAdapter(adapter);
        //加载更多
        listView.setPullLoadEnable(true);
        //刷新
        listView.setPullRefreshEnable(true);

        //刷新事件
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(page,false);
            }

            @Override
            public void onLoadMore() {
            page++;
            getData(page,true);
            }
        });
        //getData
        getData(1,false);

    }
        //getData
    private void getData(final int page,final boolean b) {
        new AsyncTask(){

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

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                News news=  gson.fromJson(s,News.class);
                if (!b){
                    list.clear();
                }
                if(news.getData() == null){
                    return;
                }

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(b){
                            listView.stopLoadMore();
                        }else {
                            listView.stopRefresh();
                            listView.setRefreshTime("刚刚");
                        }

                    }
                },2000);

                list.addAll(news.getData());
                adapter.notifyDataSetChanged();

            }
        }.execute(url+page);
    }
}

你可能感兴趣的:(Fragment的显示页面刷新使用)