我知道这听起来像是一个可怕的问题,但我一直在尽可能多地研究这个问题。我有一个应用程序需要视图寻呼机水平滚动以显示不同的视图。在每个视图中,它都需要功能,对于一个视图,它可能只是按下一个按钮,另一个视图需要从服务器下载数据(例如,检索最新的 Twitter 提要)以及其他功能。要点是在 View Pager 中,每个视图都需要功能。
我最初的想法是遵循本教程:
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 兼容,显然这是针对 Honeycomb 和 Ice Cream Sandwich。