我正在开发一个带有 android 2.1 包的应用程序。我正在使用 ActionBarSherlock 来获取操作栏。然后我尝试使用一个 Viewpager 和 multilpe Fragments Class 并拥有自己的布局。
但我得到的只是第一个片段。
我也使用了这个问题中给出的建议,但没有效果 请参阅此处的问题
这是我的带有 ActionBar 和 Viewpager 的 MainActivity。
public class MainActivity extends SherlockFragmentActivity {
private static final int NUMBER_OF_PAGES = 2;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
ActionBar actionBar;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
//actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);
viewPager = (ViewPager) findViewById(R.id.pager);
//viewPager.setOffscreenPageLimit(4);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mMyFragmentPagerAdapter);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
Fragment f = new Fragment();
Log.d("index ", ""+index);
switch (index) {
case 0:
f = PageFragment.newInstance(index);
break;
case 1:
f = FragmentTwo.newInstance(index);
break;
default:
break;
}
return f;
}
@Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
}
这是我的一个片段类。
public class PageFragment extends SherlockFragment {
public static PageFragment newInstance(int title) {
PageFragment pageFragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putInt("title", title);
pageFragment.setArguments(bundle);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("View # 01");
return view;
}
}
这是我在 activity_main.xml XML 中的片段之一
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment android:name="com.mzubair.test.PageFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.mzubair.test.FragmentTwo"
android:id="@+id/headlines_fragment2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />