我正在使用默认的Swipe Tab Fragment代码,Android启动时启动一个新的Android应用程序。我已经想出了如何修改我的数据并使用选项卡,但我还没有想出如何添加一个简单的片段到已有的。我想在选项卡的顶部或正下方添加一个信息栏。我相信这很简单,我只是不知道怎么大声笑。我通过实例学习。如何将一个片段添加到ViewPager? addView崩溃我的应用程序
我试着添加viewpager视图,但我的应用程序崩溃了,我相信我完全做错了。
在我的MainActivity: ...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
SubBarFragment infobar = new SubBarFragment();
mViewPager.addView(infobar.getView());
.....[extra code for menus and things]
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
....[code to show specific tabs and return the tab's fragment]
infobarfragment.java
public class InfobarFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.infobar, container, false);
return view;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
infobar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_wifi" />
<TextView
android:id="@+id/statusText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Wifi Status Information" />
</LinearLayout>
你有一个适配器的ViewPager,使用方法getItem/instanciateItem(取决于你使用的是:碎片还是简单的视图)。这些方法为viewpager对象提供了所有需要的视图。 – Andreas 2013-04-26 08:47:30
你可以在适配器中返回多个片段吗?我已经想到如何使用Santhosh的帮助来添加片段,但它只是将它视为我可以滑过的另一个“页面”。无论我在哪个网页,我都希望它持久。我曾希望它在标签上方,但我认为这不可能。因此,现在我猜,让它下面的标签是好的。思考? – CREW 2013-04-26 11:15:47