PullToRefreshListView使用 package com.bwie.fragment; import android.os.AsyncTask; import android.os.Bundle; 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.adapter.PlvAdapter; import com.bwie.bean.Bean; import com.bwie.pulltorefresh_test01.R; import com.bwie.utils.Utils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.List; /** * Created by zhan on 2018/1/8. */ public class Fragment01 extends Fragment { private View view; private PullToRefreshListView plv; String url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum="; private int page=0; private List<Bean.DataBean> list; private PlvAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01, container, false); initView(view); getNetData(); plv.setMode(PullToRefreshBase.Mode.BOTH); plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { list.clear(); getNetData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { page++; getNetData(); adapter.notifyDataSetChanged(); } }); return view; } private void getNetData() { new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... strings) { String json = new Utils().getJson(url+page); return json; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData(); adapter = new PlvAdapter(getActivity(), list); plv.setAdapter(adapter); adapter.notifyDataSetChanged(); plv.onRefreshComplete();//关闭刷新头和底部; } }.execute(); } private void initView(View view) { plv = (PullToRefreshListView) view.findViewById(R.id.plv); } } xlistview的使用package com.bwie.fragment; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.bawei.wzq.wzlibrary.view.XListView; import com.bwie.adapter.PlvAdapter; import com.bwie.bean.Bean; import com.bwie.utils.Utils; import com.bwie.pulltorefresh_test01.R; import com.google.gson.Gson; import java.util.List; /** * Created by zhan on 2018/1/5. */ public class Fragment02 extends Fragment implements XListView.IXListViewListener{ String json_url = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum="; int page=1; private View view; private XListView xlv; private List<Bean.DataBean> data; private Bean bean; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment02, container, false); initView(view); getData(); return view; } private void initView(View view) { xlv = (XListView) view.findViewById(R.id.xlv); xlv.setPullLoadEnable(true); xlv.setPullRefreshEnable(true); xlv.setXListViewListener(this); } @Override public void onRefresh() { data.clear(); getData(); } @Override public void onLoadMore() { page++; getData(); } public void getData(){ new AsyncTask<String, Integer, String>() { private PlvAdapter adapter; @Override protected String doInBackground(String... strings) { String jsonstr = new Utils().getJson(json_url+page); return jsonstr; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); bean = gson.fromJson(s, Bean.class); data = bean.getData(); adapter = new PlvAdapter(getActivity(), data); xlv.setAdapter(adapter); adapter.notifyDataSetChanged(); uiComplete(); } }.execute(); } public void uiComplete(){ xlv.stopRefresh(); xlv.stopLoadMore(); xlv.setRefreshTime("刚刚"); } }
PullToRefreshListView与xlistview的简单使用
最新推荐文章于 2019-01-20 08:48:45 发布