修改后的Pull

Main方法

public class Fragment1 extends Fragment{
    private PullToRefreshListView pull_listview;
    private static final String JSON_URL = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    int Nam = 1;
    private Superclass superclass;
    private List list = new ArrayList<>();
    private NetWork netWork;
    private MyAdapter adapter;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       View view = inflater.inflate(R.layout.fragment1,container,false);

        pull_listview = view.findViewById(R.id.pull_listview);

        adapter = new MyAdapter(list,getActivity());
        pull_listview.setAdapter(adapter);
        initData();



        pull_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                list.clear();;

                Nam=1;

                initData( );

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pull_listview.onRefreshComplete();
                    }
                },500);
                Toast.makeText(getActivity(),"下拉刷新",Toast.LENGTH_SHORT).show();
            }
        });

        pull_listview.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                Nam++;

                initData();

                Toast.makeText(getActivity(),"上拉加载",Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    private void initData() {
        new MyAsy().execute(JSON_URL+Nam);
    }
    private class MyAsy extends AsyncTask{

        @Override
        protected String doInBackground(String... strings) {
            String str = new NetWork().getJsonUrl(JSON_URL+Nam);
            return str;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Superclass superclass = gson.fromJson(s,Superclass.class);
            List list1 = superclass.getData();
            list.addAll(list1);
            adapter.notifyDataSetChanged();;
        }
    }
}

XML方法




    





你可能感兴趣的:(修改后的Pull)