2012-03-25 51 views
2

我在我的viewpager實現中充氣Views(而不是片段)。 我的主類擴展了Activity。在ViewPager中找不到ListView查看inflater實現

viewpager的第1頁膨脹了由textviews組成的視圖。第2頁誇大了achartengine餅圖。一切運作良好。第3頁應該用自定義適配器來擴充列表視圖。但是,由於我只擴展了Activity,所以setListAdapter不可用。

如果更改的主類來擴展ListActivity,那麼整個活動上負荷,因爲它不能找到android.R.id.list視圖(因爲它尚未膨脹)

失敗這是實例化的代碼我使用

public class SummaryPagerAdapteri extends PagerAdapter{ 

    @Override 
    public int getCount() { 
     return 5; 
    } 

    @Override 
    public Object instantiateItem(View collection, int position) { 

     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     int resId = 0; 
     View view = null; 
     switch (position) { 
     case 0: 
      //find and inflate the view 
      resId = R.layout.recordsummary_one; 
      view = inflater.inflate(resId, null); 

    //do stuff 
      break; 
     case 1: 
    //find and inflate the view 
      resId = R.layout.recordsummary_two; 
      view = inflater.inflate(resId, null); 
      Context ctx = view.getContext(); 
      //do stuff 
      break; 
     case 2: 
      resId = R.layout.recordsummary_three; 
      view = inflater.inflate(resId, null); 
      Context tctx = view.getContext(); 
      if(pdn != null){ 
      adapter = new SummaryNotesAdapter(tctx,pdn); 
      setListAdapter(adapter); 
      }     
      break; 
     case 3: 
      resId = R.layout.recordsummary_four; 
      view = inflater.inflate(resId, null); 
    //ToDo 
      break; 
     case 4: 
      resId = R.layout.recordsummary_five; 
      view = inflater.inflate(resId, null); 
    //ToDo 
      break; 
     } 

     ((ViewPager) collection).addView(view, 0); 

     return view; 
    } 

    @Override 
    public void destroyItem(View arg0, int arg1, Object arg2) { 
     ((ViewPager) arg0).removeView((View) arg2); 

    } 

    @Override 
    public boolean isViewFromObject(View arg0, Object arg1) { 
     return arg0 == ((View) arg1); 

    } 

    @Override 
    public Parcelable saveState() { 
     return null; 
    } 
} 

我如何能使用setListAdapter(適配器)任何想法不延長的ListView,或者我怎麼能不活動失敗時,它無法找到充氣的視圖擴展的ListView第一次加載?

非常感謝

戴夫

回答

1

充氣3頁定期ListView和調用ListView.setAdapter()設置適配器。在你的代碼這要麼是:

ListView listView = (ListView) inflater.inflate(resId, null); 
listView.setAdapter(adapter); 

view = inflater.inflate(resId, null); 
ListView listView = (ListView) view.findViewById(R.id.id_of_your_listview); 
listView.setAdapter(adapter); 
+0

感謝AdamK,工作的魅力。我使用了第二個選項。在使用它時,我必須將佈局文件中的列表視圖ID從'@android:id/list'更改爲'@ + id/whatever_you_want' – DaveSav 2012-03-25 21:12:13