2016-06-16 51 views
0

我有一個包含列表視圖,其中包含一些來自服務器的數據,我在listview底部添加progressbar頁腳,當用戶向下滾動listview時,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(); 
      } 

     } 
    } 
} 

,這裏是我的,當進度頁腳顯示用戶在列表視圖發送請求的代碼

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

回答

0

所有你需要做的是保持網絡請求標誌和noMoreDataLeft標誌。

boolean noMoreDataLeft; 
boolean requestGoingOn; 

每次進行網絡調用時,只需將requestGoingOn的值更改爲true即可。當你通過你的api知道在服務器上沒有更多的數據時,使noMoreDataLeft爲真。

現在定義兩個常量用來顯示加載和數據行 -

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,然後返回類型作爲加載。

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

就是這樣,現在itemType將可用,因此您可以決定需要顯示哪個視圖。希望它會幫助:)

+0

請編輯我的代碼 – vishwas