public class ShowActivity extends AppCompatActivity implements NetUtils.NetCallback, AdapterView.OnItemClickListener { private PullToRefreshListView pullList; private List<Result.ResultBean.DataBean> dataBeans; private NetUtils instance = NetUtils.getInstance(); private String url = "http://172.17.8.100/mobile/exam/findNewList"; private MyPullAdatper adatper; private boolean isPull = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); //初始化控件 pullList = findViewById(R.id.pull_list); //设置监听,模型 pullList.setMode(PullToRefreshListView.Mode.BOTH); pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { isPull = true; loadData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { loadData(); } }); //加载数据 loadData(); //获取到数据添加到集合中 instance.setCallback(this); //获取到空的adapter, dataBeans = new ArrayList<>(); adatper = new MyPullAdatper(this, dataBeans); pullList.setAdapter(adatper); //点击监听 pullList.setOnItemClickListener(this); } private void loadData() { instance.getDataFromServer(url); } @Override public void onSuccess(String result) { Log.i("aaa", result); Gson gson = new Gson(); Result result1 = gson.fromJson(result, Result.class); dataBeans.addAll(result1.getResult().getData()); //下拉 if (isPull) { isPull = !isPull; dataBeans.clear(); } //更新数据 adatper.notifyDataSetChanged(); //停止更新 pullList.onRefreshComplete(); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String url1 = dataBeans.get(position-1).getUrl(); Intent intent = new Intent(ShowActivity.this, WebActivity.class); intent.putExtra("url", url1); startActivity(intent); } }
public class FragmentOne extends Fragment { private ViewPager bunner; private PullToRefreshListView pullList; private NetUtils instance = NetUtils.getInstance(); private int page = 1; private String url = "https://www.apiopen.top/satinApi?type=1&page="+ page +""; private List<Beans.DataBean> dataBeans; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_layout01, container, false); //初始化控件 bunner = view.findViewById(R.id.bunner); pullList = view.findViewById(R.id.pull_list); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //设置模型,并监听 pullList.setMode(PullToRefreshListView.Mode.BOTH); pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { page = 1; loadData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { page++; loadData(); } }); //设置一个空的adapter dataBeans = new ArrayList<>(); final MyPullAdapter adapter = new MyPullAdapter(getActivity(), dataBeans); pullList.setAdapter(adapter); //加载数据 loadData(); instance.setCallback(new NetUtils.NetCallback() { @Override public void onSuccess(String result) { //解析并处理数据 Gson gson = new Gson(); Beans beans = gson.fromJson(result, Beans.class); if (page == 1) { dataBeans.clear(); } //添加请求数据到集合 dataBeans.addAll(beans.getData()); //适配器改变刷新后的内容 adapter.notifyDataSetChanged(); //停止刷新 pullList.onRefreshComplete(); } }); } /** * 请求数据的方法 */ private void loadData() { instance.getDataFromServer(url); } }