PullToRefreshGridView上拉重新整理下拉載入
阿新 • • 發佈:2018-11-08
PullToRefreshGridView上拉重新整理下拉載入
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout --> <com.handmark.pulltorefresh.library.PullToRefreshGridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2"></com.handmark.pulltorefresh.library.PullToRefreshGridView>
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import java.util.ArrayList;
public class TwoFragment extends Fragment {
private int cc = 1; private String path = "https://www.apiopen.top/meituApi?page=" + cc; private PullToRefreshGridView grid; private ArrayList<Bean.DataBean> list = new ArrayList<Bean.DataBean>(); private GridView mGridView; private Bean bean; private Adapteraa adapteraa; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_two, container, false); grid = view.findViewById(R.id.gridView); grid.setMode(PullToRefreshBase.Mode.BOTH); mGridView = grid.getRefreshableView(); sta(cc); grid.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) { sta(cc); new GetDataTask().execute(); } @Override public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { cc++; sta(cc); new GetDataTask().execute(); } }); return view; } public void sta(int cc) { Gong gong = new Gong(); gong.get(path + cc).getHome(new Gong.HttpGong() { @Override public void getName(String name) { Gson gson = new Gson(); //list.clear(); bean = gson.fromJson(name, Bean.class); list.addAll(bean.getData()); adapteraa = new Adapteraa(getActivity(), list); mGridView.setAdapter(adapteraa); } }); } private class GetDataTask extends AsyncTask<Void, Void, String[]> { @Override protected String[] doInBackground(Void... params) { try { Thread.sleep(2000); } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(String[] result) { list.addAll(bean.getData()); adapteraa.notifyDataSetChanged(); grid.onRefreshComplete(); super.onPostExecute(result); } }
}