2012-04-10 15 views
1

我知道這可能聽起來像一個可怕的問題,但我一直在研究儘可能多的解決方案。我有一個應用程序需要查看傳呼機水平滾動顯示不同的意見。在每個視圖中,它需要功能,對於一個視圖來說可能只是按下一個按鈕,另一個視圖需要從服務器下載數據(例如,獲取最新的Twitter提要)以及其他功能。主要觀點是在View Pager中,每個視圖都需要功能。在Android中創建ViewPager或等效的WITH功能

我最初的想法是按照這個教程:

http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

然而,這僅僅是提供具有無相互作用的觀點。我設法實現了這一點,並添加了我的佈局,但是這隻能解決一半的問題。它顯示瞭如何在評論中添加基本操作,如單個按鈕。我希望每個觀點都有自己的活動,這是能夠做出自己獨特的事情。

這是我本來有:

public class DashboardContainerActivity extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    //Set content view to the dashboard container xml view 
    setContentView(R.layout.dashboard_container); 

    //Create a new instance of my page adapter from below 
    MyPageAdapter adapter = new MyPageAdapter(); 
    //Reference the view pager used in the dashboard container xml view 
    ViewPager myPager = (ViewPager) findViewById(R.id.dashboardpanelpager); 
    //set an adapter to the view pager 
    myPager.setAdapter(adapter); 
    //First panel to be shown when opened 
    myPager.setCurrentItem(3); 
    } 

} 

/*------------------------------------------------------*/ 
class MyPageAdapter extends PagerAdapter { 

    public int getCount() { 
     //Return 7 as there will be 7 panes in the dashboard 
     return 7; 
    } 

    public Object instantiateItem(View collection, int position) { 

     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = null; 
     switch (position) { 
     case 0: 
      v = inflater.inflate(R.layout.dashboard_social, null); 
      break; 
     case 1: 
      v = inflater.inflate(R.layout.dashboard_info, null); 
      //DISPLAY INFORMATION FROM SERVER AND DISPLAY HERE 
      break; 
     case 2: 
      v = inflater.inflate(R.layout.dashboard_allcourses, null); 
      //LIST OF COURSES 

      break; 
     case 3: 
      v = inflater.inflate(R.layout.dashboard_news, null); 
      //USE HTTP HERE FOR TWITTER FEED 


      break; 
     case 4: 
      v = inflater.inflate(R.layout.dashboard_mycourse, null); 
      //DOWNLOAD USER'S PERSONAL INFORMATION AND DISPLAY HERE 

      break; 
     case 5: 
      v = inflater.inflate(R.layout.dashboard_media, null); 
      //DISPLAY LATEST UPLOADED MULTIMEDIA 
      break; 
     case 6: 
      v = inflater.inflate(R.layout.dashboard_extras, null); 
      break; 
     } 

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

     return v; 
    } 

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

我有添加到ViewPager每個佈局獨立的活動,但我猜測,這些活動不能被添加到ViewPager而不僅僅是佈局。

我已經閱讀了關於碎片的一些內容,但我不確定這是否與API級別8兼容,顯然這是針對蜂窩和冰淇淋三明治。

回答

3

這是不可能有一個活動作爲ViewPager的一部分,但沒有理由爲什麼你不能在你的ViewPager添加你所描述的每一頁的功能。要分配在每個視圖的互動或事件的部件只是添加正確的聽衆instantiateItem()在每種情況下聲明:

case 0: 
     v = inflater.inflate(R.layout.dashboard_social, null); 
     Button myButton = (Button) v.findViewById(R.id.name_of_button_in_social_dashboard); 
     myButton.setOnClickListener(...); 
     break; 

對於其他任何互動,喜歡把自己的http請求的Twitter的飼料,只執行那些爲你的主要活動的一部分(類似http請求應當在後臺線程中完成)。當您想要更新Twitter頁面中的UI時,只需使用ViewPager.getChildAt(3)來獲取子元素。把你的Activity看作只有7個兒童視圖的大布局,它們一次都可用(但用戶只能在他們滑動時看到它們)。

綜上所述,更好的設計模式可能是使用支持ViewPager的FragmentPagerAdapter碎片。這允許各種頁面到不同的類別的更好邏輯分解 - 片段也提供其他用途,如能夠爲較大的屏幕布局(片)在一次加載屏幕上的多個。

像ViewPager,片段中的所有可用的方式回到通過支持庫API等級4(見Fragment)。所以你不必擔心向後兼容性。