如何使用listview底部的进度条显示更多页脚?

时间:2016-06-16 03:17:54

标签: android

我有一个包含listview的片段,其中包含来自服务器的一些数据,当用户向下滚动列表视图时,我在listview底部添加了progressbar页脚,listview底部的进度条显示给用户并发送服务器请求并添加更多内容列表视图中的数据,问题是当滚动到结束进度条时也可以看到但是发送服务器请求背靠背。我如何能够解决这个问题。

这是我的listview滚动代码

@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-1 && 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();
            }

        }
    }
}

这是我在listview中向用户显示进度条页脚时发送请求的代码

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());
                        m_ListView.removeFooterView(mFooter);
                        requestQueue.cancelAll(TAG);
                    } 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();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要做的就是为网络请求维护一个标志,为noMoreDataLeft维护一个标志。

boolean noMoreDataLeft;
boolean requestGoingOn;

每次拨打网络电话时,只需将requestGoingOn的值更改为true即可。当你通过api知道服务器上没有更多数据时,noNoreDataLeft就是真的。

现在为show loading和data row定义两个常量 -

private final static int TYPE_LOADING = 0;
private final static int TYPE_DATA = 1;

现在 -

@Override
public int getItemCount() {
    return data.size() + (requestGoingOn && !isNoMoreDataLeft ? 1 : 0);
}

请求进行时会添加更多行。现在你只需要检查当前位置是否超过data.size然后返回类型为loading。

    @Override
    public int getItemViewType(int position) {
        return position >= data.size() ? TYPE_LOADING : TYPE_DATA;
    }

就是这样,现在itemType将可用,因此您可以决定需要显示哪个视图。希望它会有所帮助:)