我知道這可能聽起來像一個可怕的問題,但我一直在研究儘可能多的解決方案。我有一個應用程序需要查看傳呼機水平滾動顯示不同的意見。在每個視圖中,它需要功能,對於一個視圖來說可能只是按下一個按鈕,另一個視圖需要從服務器下載數據(例如,獲取最新的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兼容,顯然這是針對蜂窩和冰淇淋三明治。