多条加载获取数据,设置适配器,上拉下拉加载

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值