如何将一个片段添加到ViewPager? addView崩溃我的应用程序

我使用Android启动的默认Swipe Tab Fragment代码,当您启动一个新的Android应用程序。 我已经想出了如何修改我的数据和使用标签,但我还没有想出如何添加一个简单的片段,已经有了。 我想添加一个信息栏在标签的顶部或正下方。 我确定这很简单,我只是不知道如何大声笑。 我通过例子来学习。

我已经尝试添加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> 

Solutions Collecting From Web of "如何将一个片段添加到ViewPager? addView崩溃我的应用程序"

你可以按照这个方法:

只是在分页器中为每个视图使用片段。

FragmentActivity onCreate()方法中写下面的代码。

 List<Fragment> fragments = new Vector<Fragment>(); //for each fragment you want to add to the pager Bundle page = new Bundle(); page.putString("url", url); fragments.add(Fragment.instantiate(this,MyFragment.class.getName(),page)); //after adding all the fragments write the below lines this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); mPager.setAdapter(this.mPagerAdapter); 

示例片段定义:

 public class MyFragment extends Fragment { public static MyFragment newInstance(String imageUrl) { final MyFragment mf = new MyFragment (); final Bundle args = new Bundle(); args.putString("somedata", "somedata"); mf.setArguments(args); return mf; } public MyFragment() {} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String data = getArguments().getString("somedata"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate and locate the main ImageView final View v = inflater.inflate(R.layout.my_fragment_view, container, false); //... return v; } 

FragmentPagerAdapter

 public class MyFragmentAdapter extends FragmentPagerAdapter { public static int pos = 0; private List<Fragment> myFragments; public MyFragmentAdapter(FragmentManager fm, List<Fragment> myFrags) { super(fm); myFragments = myFrags; } @Override public Fragment getItem(int position) { return myFragments.get(position); } @Override public int getCount() { return myFragments.size(); } @Override public CharSequence getPageTitle(int position) { setPos(position); String PageTitle = ""; switch(pos) { case 0: PageTitle = "page 1"; break; case 1: PageTitle = "page 2"; break; case 2: PageTitle = "page 3"; break; case 3: PageTitle = "page 4"; break; case 4: PageTitle = "page 5"; break; case 5: PageTitle = "page 6"; break; case 6: PageTitle = "page 7"; break; } return PageTitle; } public static int getPos() { return pos; } public static void setPos(int pos) { MyFragmentAdapter.pos = pos; } }