PullToRefreshListView外掛 載入重新整理
public class fragment1 extends Fragment {
private String apiurl = “http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=10&page=”;
private PullToRefreshListView lv;
private int page;
private Madapter madapter;
private final int count = 10;
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null, false);
lv = view.findViewById(
madapter = new Madapter(getActivity());
lv.setAdapter(madapter);
lv.setMode(PullToRefreshBase.Mode.BOTH);
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
page = 1;
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
initData();
}
});
page = 1;
initData();
return view;
}
@SuppressLint(“StaticFieldLeak”)
private void initData() {
new AsyncTask<String, Void, ArrayList<ShuBean.news>>() {
private ArrayList<ShuBean.news> result; private ShuBean shuBean; @Override protected ArrayList<ShuBean.news> doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = urlConnection.getInputStream(); Gson gson = new Gson(); ShuBean shuBean = gson.fromJson(new BufferedReader(new InputStreamReader(inputStream)), ShuBean.class); result = shuBean.getResult(); return result; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(ArrayList<ShuBean.news> resultBeans) { if (resultBeans == null) { for (int i=0;i<result.size();i++){ } lv.onRefreshComplete(); return; } if (page == 1) { madapter.setList(resultBeans); } else { madapter.addList(resultBeans); } lv.onRefreshComplete(); page++; } }.execute(apiurl + page); }