PullToRefreshGridView的简单使用

PullToRefreshGridView是可以实现上拉加载和下拉刷新的第三方工具中的一种 

可以使我们的GridView实现上拉下载和下拉刷新的功能,

如果想要使用

PullToRefreshGridView,那么首先得导入依赖


接下来是布局layout

PullToRefreshGridView的简单使用_第1张图片

接下来就回到我们的Activity中来进行对PullToRefreshGridView的操作

(我用了一个Fragment类的操作方法,可能与Activity操作不太一样)

public class show_fragment2 extends Fragment{

    private PullToRefreshGridView pullToRefreshGridView;
    private HttpUtils httpUtils = HttpUtils.getInstance();
    private String path = "http://gank.io/api/data/福利/10/";
    private int page = 1;
    private PageTwoBase adapter;
    private List list = new ArrayList<>();
    private Handler handler = new Handler();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

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

        initView(view);
        getDataL();
        return view;
    }

    private void initView(View view) {
        pullToRefreshGridView = view.findViewById(R.id.gridview);

        pullToRefreshGridView.setMode(PullToRefreshBase.Mode.BOTH);

        ILoadingLayout start = pullToRefreshGridView.getLoadingLayoutProxy(true,false);
        start.setPullLabel("下拉刷新");
        start.setRefreshingLabel("正在刷新");
        start.setReleaseLabel("刷新完成");
        ILoadingLayout end = pullToRefreshGridView.getLoadingLayoutProxy(false,true);
        end.setPullLabel("上拉下载");
        end.setRefreshingLabel("正在加载");
        end.setReleaseLabel("加载完成");

        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(final PullToRefreshBase pullToRefreshBase) {
                    page = 1;
                    getDataL();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pullToRefreshGridView.onRefreshComplete();
                        }
                    },2000);
            }

            @Override
            public void onPullUpToRefresh(final PullToRefreshBase pullToRefreshBase) {
                    page += 1;
                    getDataL();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pullToRefreshGridView.onRefreshComplete();
                        }
                    },2000);
            }
        });
        adapter = new PageTwoBase(getActivity(),list);
        pullToRefreshGridView.setAdapter(adapter);

    }

    public void getDataL(){
        String url = path + page;
        httpUtils.getData(url);
        httpUtils.Huidiao(new HttpUtils.JieKou() {
            @Override
            public void getjsondata(String json) {
                Log.i("TAG",json);
                Gson gson = new Gson();
                PageTwoBean pageTwoBean = gson.fromJson(json, PageTwoBean.class);
                List data = pageTwoBean.getResults();
                if (page == 1){
                    list.clear();
                }
                list.addAll(data);
                Log.i("TAG",list.size()+"ssss");
                adapter.notifyDataSetChanged();
            }
        });


    }
}

你可能感兴趣的:(PullToRefreshGridView的简单使用)