2013-06-28 22 views
0

我使用以下爲我的ListView代碼..無盡的ListView表現得像一個簡單的ListView

FragmentActivity

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
      View view = inflater.inflate(R.layout.home, container, false);  
      listView = (ListView) view.findViewById(R.id.listView); 
      listView.setAdapter(new CustomArrayAdapterForHome(mContext,questions)); 

      return view; 
    } 

這裏是ListView的

@SuppressLint("DefaultLocale") 
    public class CustomArrayAdapterForHome extends EndlessAdapter 
    { 
     private final LayoutInflater inflator; 
     protected ImageLoader imageLoader; 
     private DisplayImageOptions options; 

     private RotateAnimation rotate=null; 
     private View pendingView = null; 

     public CustomArrayAdapterForHome(Context ctx,ArrayList<Question> questionList) 
     { 
      super(new ArrayAdapter<Question>(ctx, R.layout.question_adapter_layout, questionList)); 

      inflator = mContext.getLayoutInflater(); 
      imageLoader = ImageLoader.getInstance(); 

      options = new DisplayImageOptions.Builder() 
       .cacheInMemory() 
       .cacheOnDisc() 
       .showImageForEmptyUri(R.drawable.user_male) 
       .displayer(new RoundedBitmapDisplayer(5)) 
       .build(); 

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

     class ViewHolder 
     { 
      // MY CODE 
     } 

     @Override 
     public int getCount() { 
      return questions.size(); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     {  
      final Question question = questions.get(position); 
      final OpenionUser myUser = question.getUser(); 
      // MY CODE 


      return view; 
     } 



      @Override 
      protected View getPendingView(ViewGroup parent) 
      { 
      View row = LayoutInflater.from(parent.getContext()).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); 
      startProgressAnimation(); 

      return(row); 
      } 


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

     @Override 
     protected void appendCachedData() 
     { 

     } 

     @Override 
     protected boolean cacheInBackground() throws Exception 
     { 
      getQuestions(); 
      return true; 
     } 
    } 

適配器上面的代碼只是表現得像簡單的ListView,cacheInBackground或getPendingView沒有被調用。 FurtherMore我想添加一個headerView也不工作。

我在這裏遺漏了什麼?

回答

1

你在這裏的大部分代碼不屬於EndlessAdapter。引用the documentation

它被設計成環繞另一個適配器,在那裏有你的「真實」數據。因此,它遵循Decorator模式,用新的Endless Technology(TM)增強您的當前適配器。

因此,首先,創建一個定期ArrayAdapter,並得到所有你想要的(例如,你的getView()實現)的造型和材料。然後,創建一個添加了「無限性」的子類。

值得注意的是:

  • EndlessAdapter子類不應重寫getView(),因爲這是在無盡的行爲被添加

  • EndlessAdapter子類不應重寫getCount(),因爲這是由管理EndlessAdapter執行本身

您可能希望檢查the sample app以瞭解它是如何實現EndlessAdapter的子類。

或者,由於EndlessAdapterwill not work with header views,您可能只需切換到不同的無限列表解決方案或自行打印。

+0

好吧,我已經做了工作,感謝上述建議。請問一個小問題。我在cacheInBackground中做getQuestions()。和adapter.setRunInBackground(false);在我getQuestion()異步任務後,我得到修改數組readyListener.onItemsReady(問題);工作正常。但是如果我沒有得到任何東西,我的數組沒有被修改,cacheInBackground會無限重複。該怎麼辦? –

+0

@MuhammadUmar:當你到達數據結尾時,你應該從'cacheInBackground()'返回'false'。 – CommonsWare