2014-03-12 51 views
0

我試圖在應用程序中實現無限列表,其中您從webservice獲得前25個結果,並在它到達列表底部時加載下一個25等,直到達到總結果。但是我迷失在無盡列表適配器如何知道我何時到達列表底部?我從cwac看了DemoAdapter和EndlessAdapterDemo例子,但無法弄清楚..或者它與我的Activity不是一個ListActivity(我認爲不是)?前25個結果加載到列表中,但之後我需要更改調用參數來調用下一個25我不使用asyntask(使用其他方法)獲取結果,或者我錯誤地解釋了在cacheInBackground中使用asnytask的指南?但是什麼時候以及如何來到無盡的適應?下面是我實現的適配器:無盡列表適配器CWAC與ActionBarActivity協同工作,以及如何觸發列表底部的加載?

public class SearchResultEndlessAdapter extends EndlessAdapter { 

    private static final String TAG = SearchResultEndlessAdapter.class.getSimpleName(); 
    private RotateAnimation rotate = null; 
    private LayoutInflater inflater; 
    private View pendingView = null; 
    private List<KeywordSearchResults> newKsearchResultList; 
    private int limit = 0; 

    public SearchResultEndlessAdapter(SearchResultListAdapter adapter, Context ctx) { 
     super(adapter); 

     this.inflater = LayoutInflater.from(ctx); 
     rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     rotate.setDuration(600); 
     rotate.setRepeatMode(Animation.RESTART); 
     rotate.setRepeatCount(Animation.INFINITE); 
    } 

    /** 
    * Set the new list that is loaded from the webservice 
    * 
    * @param newKsearchResultList 
    *   List<KeywordSearchResults> 
    */ 
    public void setNewData(List<KeywordSearchResults> newKsearchResultList) { 

     this.newKsearchResultList = new ArrayList<KeywordSearchResults>(newKsearchResultList); 
    } 

    /** 
    * The value of total results 
    * 
    * @param limit 
    *   int 
    */ 
    public void setLimit(int limit) { 

     this.limit = limit; 
    } 

    @Override 
    protected void appendCachedData() { 
     LogService.log(TAG, "appendCachedData"); 

     if (getWrappedAdapter().getCount() < limit) { 

      SearchResultListAdapter srListAdapter = (SearchResultListAdapter) getWrappedAdapter(); 
      for (KeywordSearchResults kws : newKsearchResultList) { 
       srListAdapter.add(kws); 
      } 
     } 

    } 

    @Override 
    protected boolean cacheInBackground() throws Exception { 

     LogService.log(TAG, "cacheInBackground"); 
     return (getWrappedAdapter().getCount() < limit); 
    } 

    @Override 
    protected View getPendingView(ViewGroup parent) { 

     LogService.log(TAG, "getPendingView"); 
     View row = inflater.inflate(R.layout.row, null); 

     pendingView = row.findViewById(android.R.id.text1); 
     pendingView.setVisibility(View.GONE); 
     pendingView = row.findViewById(R.id.throbber); 
     pendingView.setVisibility(View.VISIBLE); 
     pendingView.startAnimation(rotate); 
     startProgressAnimation(); 

     return (row); 
    } 

    public void startProgressAnimation() { 
     if (pendingView != null) { 
      pendingView.startAnimation(rotate); 
     } 
    } 
} 

而且從myActivity部分延伸ActionBarActivity:

//in oncreate 
searchResultEndlessAdapter = new SearchResultEndlessAdapter(searchResultadapter,this); 
    searchResultEndlessAdapter.setRunInBackground(false); 
    mListView.setAdapter(searchResultEndlessAdapter); 
    mListView.setOnItemClickListener(this); 
    mListView.setOnScrollListener(this); 

//callbacks of scroll 
    @Override 
    public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) { 
    //something needs to be done here with endlessAdapter ? 

    } 

    @Override 
    public void onScrollStateChanged(AbsListView arg0, int arg1) { 

} 
+1

請注意,我已經退休了該項目。 – CommonsWare

+0

我明白了..那麼不禁要如何開始呢?我想我錯過了一些非常簡單的事情。 – cesztoszule

+0

@CommonsWare順便說一句:我想實現無盡的listview模式,並且找不到退休背後的原因。該項目是由一些谷歌官方解決方案取代,還是剛剛達到穩定的「沒有其他要添加」的狀態?你還認爲這是該模式的推薦方法嗎? – Piovezan

回答

0

那麼不能使它與cwacs適配器工作..正常進行了。

@Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
     int loadedItems = firstVisibleItem + visibleItemCount; 

     if ((loadedItems == totalItemCount) && !isloading && totalItemCount > 1) { 
      if (!isloading) { 
       isloading = true; 
       if (!((start - 1) == total)) { 
        // call ws 
        search(); 
       } 
      } 
     } 
    } 

    @Override 
    public void onScrollStateChanged(AbsListView arg0, int arg1) { 

    } 
0

您也可以使用EndlessAdapter和ActionBarActivity。你所需要做的就是在Activity中改變你的適配器,它將ActionBarActivity擴展到你所做的SearchResultEndlessAdapter。確保你正在做構造函數。

例如,這是在我的活動延伸ActionBarActivity:

     ... 

        if (adapter == null) { 
         ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>(); 

         items.addAll(questionsList); 

         adapter = new CustomAdapter(this, items); 
         } 
         else { 
         adapter.startProgressAnimation(); 
         } 

         listView.setAdapter(adapter); 

         ... 
相關問題