控件:ListView
<com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/plv" xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" ></com.handmark.pulltorefresh.library.PullToRefreshListView>
private View view; private PullToRefreshListView pulllist; int pid=1; BaseList base; List<MyDeteBean.ResultsBean> results=new ArrayList<>(); private ILoadingLayout loadingLayoutProxy; private ILoadingLayout load; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_layout,container,false); pulllist = view.findViewById(R.id.plv); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); sta(); //设置可以上拉加载下拉刷新 pulllist.setMode(PullToRefreshBase.Mode.BOTH); //设置滑动时文字 loadingLayoutProxy = pulllist.getLoadingLayoutProxy(true, false); loadingLayoutProxy.setPullLabel("下拉刷新"); loadingLayoutProxy.setRefreshingLabel("正在拉"); loadingLayoutProxy.setReleaseLabel("放开刷新"); load = pulllist.getLoadingLayoutProxy(false, true); load.setPullLabel("上拉刷新"); load.setRefreshingLabel("正在加载"); load.setReleaseLabel("放开刷新"); //设置适配器listview pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(getActivity(),"下啦",Toast.LENGTH_SHORT).show(); xrefalsh(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(getActivity(),"上啦",Toast.LENGTH_SHORT).show(); pid++; sta(); } }); } public void sta(){ String path="http://gank.io/api/data/Android/10/"+pid; NetWorkUrl.newUrl(getActivity(), path, new NetPort() { @Override public void port(String json) { Gson gson =new Gson(); MyDeteBean bean = gson.fromJson(json, MyDeteBean.class); results.addAll(bean.getResults()); if(base==null){ base=new BaseList(getActivity(),results); pulllist.setAdapter(base); }else{ base.notifyDataSetChanged(); } pulllist.onRefreshComplete(); } }); } public void xrefalsh(){ pid--; String path="http://gank.io/api/data/Android/10/"+pid; NetWorkUrl.newUrl(getActivity(), path, new NetPort() { @Override public void port(String json) { Gson gson =new Gson(); if(json!=null){ results.clear(); MyDeteBean bean = gson.fromJson(json, MyDeteBean.class); results.addAll(0,bean.getResults()); if(base==null){ base=new BaseList(getActivity(),results); pulllist.setAdapter(base); }else{ base.notifyDataSetChanged(); }}else{ } pulllist.onRefreshComplete(); loadingLayoutProxy.setLastUpdatedLabel("上次刷新时间"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis()))+";"); } });}