PullToRefreshListView最后不满一页时动态加载逻辑处理

 
  

问题描述:

pullToRefreshListView上拉加载更多时,很多人写的项目中都是上拉加载成功pageNo++,如果最后一页不满10条数据(假设pageSize设置为10)pageNo++,那么如果服务器最后一页数据从5条实时更新到6条,继续上拉加载更多时就不能动态加载出服务器刚刚添加的数据(当然也可以不做这层处理,放弃掉动态加载最新数据)。

解决思路:

1.在上拉加载时判断返回的这页数据满不满一页,如果不满则判断当前list中是不是整数页的个数,如果不是整数页则说明最后一页数据不满一页;

2.如果1中条件成立,判断最后一页的条数和最新加载出来的一页数据数量比较,如果数量不同则更新list中最后一页数据,如果相同则提示用户“已无最新数据”;

3.如果最新一页的数据满10条则list直接添加所有新数据,pageNo++。

代码实现:


public class MFragment extends Fragment {
    private PullToRefreshListView pullToRefreshListView;
    private CreateProgressDialog mProgressDialog;
    private View.OnClickListener listener;
    private final int PAGE_SIZE = 10;
    public int currentPage = 1;
    private boolean isRequsting = false;
    private boolean isHeaderShown = false;
    private UserInfo mUserInfo;
   

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
        initData();
        currentPage = 1;
        requestMList();
    }

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        if (args != null) {
            pageIndex = args.getInt("pageIndex");
        }
    }

    private void initView() {
        mProgressDialog = new CreateProgressDialog(mActivity);
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pullToRefreshListView);
    }
   
    private void initListener() {
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override //注意:新版分两个回调方法 区分开了上拉下拉
            public void onRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                
                isHeaderShown = false;
                if (pullToRefreshListView.isHeaderShown()) {
                    currentPage = 1;
                    isHeaderShown = true;
                }
                if (!isRequsting) {
                    requestMList();
                    mAdapter.setData(mDataList);
                    Utils.showToast(mActivity, "获取数据中...");
                } else {
                    pullToRefreshListView.onRefreshComplete();
                    Utils.showToast(mActivity, "已经在加载咯...");
                }
            }
        });
        ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时,显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时,显示的提示
    }

    private void initData() {
        mProgressDialog.setCanceledOnTouchOutside(false);
        temList = new ArrayList<>();
        mDataList = new ArrayList<>();
        mAdapter = new MachineListAdapter(mActivity,pageIndex);
        mAdapter.setData(mDataList);
        pullToRefreshListView.setAdapter(mAdapter);
    }

    public void requestMList() {
        if (isRequsting)
            return;
        isRequsting = true;
        mProgressDialog.show("数据加载中~");
     
        RequestParams params = new RequestParams();
        params.put("pageNo", currentPage);
        params.put("pageSize", PAGE_SIZE);
        RequestManager.post("dManage/queryDInfo", null, params, new ResponseListener() {
            @Override
            public void requestSuccess(Object clazz) {
                super.requestSuccess(clazz);
                try {
                    temList.clear();
                    JSONObject jsonObject = new JSONObject(clazz.toString());
                    String status = jsonObject.getString("status");
                    String statusInfo = jsonObject.getString("statusInfo");
                    if ("1".equals(status)) {
                        JSONArray deviceInfoVos = jsonObject.getJSONArray("deviceInfoVos");
                        if (currentPage == 1) {
                            mDataList.clear();
                        }
                        temList = gson.fromJson(deviceInfoVos.toString(),new TypeToken<List<DeviceInfoVos>>(){}.getType());
                        int size = mDataList.size();
                        int remainder = size % PAGE_SIZE;
                        if (temList.size() == remainder) {
                            Utils.showToast(mActivity, "没有更多数据!");
                        } else {
                            if (remainder != 0) {
                                if (size > PAGE_SIZE) {//多于一页
                                    mDataList = mDataList.subList(0, size - remainder - 1);
                                } else {//如果小于1页直接清空
                                    mDataList.clear();
                                }
                            }
                            //如果是下拉刷新则清空list 重新加载数据
                            if (isHeaderShown) {
                                mDataList.clear();
                            }
                            mDataList.addAll(temList);
                            //如果数据满一整页pageNo++
                            if (temList.size() == PAGE_SIZE) {
                                currentPage++;
                            }
                            mAdapter.setData(mDataList);
                        }
                    } else {
                        mAdapter.setData(mDataList);
                        Utils.showToast(mActivity, statusInfo);
                    }
                } catch (Exception je) {
                    mDataList = temList;
                    mAdapter.setData(mDataList);
                    Utils.showToast(mActivity, "数据获取失败!");
                    je.printStackTrace();
                } finally {
                    pullToRefreshListView.getRefreshableView().setSelection(mDataList.size());
                    mAdapter.notifyDataSetChanged();
                    isRequsting = false;
                    pullToRefreshListView.onRefreshComplete();
                    if (mProgressDialog != null)
                        mProgressDialog.dismiss();
                 
                }
            }

            @Override
            public void requestError(VolleyError e) {
                isRequsting = false;
                pullToRefreshListView.onRefreshComplete();
                if (mProgressDialog != null)
                   mProgressDialog.dismiss();
                
                Utils.showToast(mActivity, "数据获取异常,请重试!");
            }
        });
    }
}

通过以上代码问题就解决了(如果有更好的实现也请不吝赐教)。

虽然有些公司对这层问题没做要求(偷偷的说一下,也可能是因为产品和测试比较水),但作为开发还是考虑周到比较好。

你可能感兴趣的:(问题解决方案,android)