滚动到列表视图末尾时如何发送服务器请求?

How to send server request when scrolled to end of the listview ?

我有一个片段,其中我有包含来自服务器的数据的列表视图,并且在列表视图的底部还有一个页脚,它还发送服务器请求以在列表视图中添加更多项目,问题是当页脚出现时,即列表视图滚动结束将服务器请求发送回 back.pleas 帮助我解决这个问题,它已经处理了两天。

滚动侦听器代码

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
        Log.i("a", "scrolling stopped...");
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    if (firstVisibleItem + visibleItemCount == totalItemCount&& totalItemCount != 0) {
        if (!isloading) {
            // It is time to add new data. We call the listener
            isloading = true;
            if (NetworkUtil.isConnected(getActivity())) {
                m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
                m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above

                sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
                sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                loadmoreData();
            } else {
                Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
            }

        }
    }
}

加载更多数据的代码

public void loadmoreData() {

    try {
        String json;
        // 3. build jsonObject
        final JSONObject jsonObject = new JSONObject();// making object of Jsons.
        jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
        jsonObject.put("pin", m_szEncryptedPassword);// put password
        jsonObject.put("recordcount", sz_RecordCount);// put record count
        jsonObject.put("lastcountvalue", sz_LastCount);// put last count
        Log.d("CAppList:",sz_RecordCount);
        Log.d("Capplist:",sz_LastCount);
        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();// convert Json object to string

        System.out.println("Server Request:-" + json);
        requestQueue = Volley.newRequestQueue(getActivity());

        jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                System.out.println("Response:-" + response);
                try {
                    JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
                    for (int i = 0; i < posts.length(); i++) {
                        JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                        item = new CDealAppDatastorage();// object create of DealAppdatastorage
                        item.setM_szHeaderText(post.getString("dealname"));//getting deal name
                        item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
                        item.setM_szDealValue(post.getString("dealvalue"));

                        if (!s_oDataset.contains(item)) {
                            s_oDataset.add(item);
                        }
                    }
                    isloading=false;
                    m_oAdapter.notifyDataSetChanged();
                    if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
                    } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
                    } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                System.out.println("Error:-" + error);
                if (error instanceof TimeoutError) {
                    CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection lost ! Please try again", getActivity());
                } else if (error instanceof NetworkError) {
                    CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No internet connection", getActivity());
                }
            }
        });
        requestQueue.add(jsonObjectRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

您可以通过以下方式进行:

private boolean bBottomOfView;

@Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && bBottomOfView) {
            if (NetworkUtil.isConnected(getActivity())) {
                m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
                m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above

                sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
                sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                loadmoreData();
            } else {
                Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
            }

        }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        bBottomOfView = (firstVisibleItem + visibleItemCount) == totalItemCount ? true : false;
}