Java Android多刷视图

Java Android多刷视图,java,android,android-layout,android-fragments,android-viewpager,Java,Android,Android Layout,Android Fragments,Android Viewpager,最近,我一直在尝试通过实现多个滑动视图来为我的应用程序创建高级外观 让我解释一下我所说的“多次滑动视图”的含义: 请原谅我的绘画技巧不好 如您所见,我只是尝试将滑动视图堆叠在彼此之下。当然,每个滑动视图包含不同的片段 我成功地创建了一个滑动视图,但这就是我所能做的。 要创建一个滑动视图,我创建了从FragmentPagerAdapter扩展的PagerAdapter类: public class SectionsPagerAdapter extends FragmentPagerAda

最近,我一直在尝试通过实现多个滑动视图来为我的应用程序创建高级外观

让我解释一下我所说的“多次滑动视图”的含义:

请原谅我的绘画技巧不好

如您所见,我只是尝试将滑动视图堆叠在彼此之下。当然,每个滑动视图包含不同的片段

我成功地创建了一个滑动视图,但这就是我所能做的。 要创建一个滑动视图,我创建了从FragmentPagerAdapter扩展的PagerAdapter类:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment; 
        switch(i){
        case 0:
             fragment = new MyFragment1();
             break;
        case 1:
             fragment = new MyFragment2();
             break;
        case 2:
             fragment = new MyFragment3();
             break;
        default:
             throw new IllegalArgumentException("Invalid section number");
        }

        return fragment;
    }}
名为“MyFragment1/2/3”的类是一个创建具有正确布局的视图的类:

    public static class MyFragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.simpletab, null);
        return v;
}
}
我创建了一个ViewPager和一个SectionsPagerAdapter

    SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
另外,在MainActivity的onCreate函数中,我添加了:

        mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.comp);
    mViewPager.setAdapter(mSectionsPagerAdapter);
这给了我一个滑动视图。 现在我试着做了两个,所以我写了另一个PagerAdapter类来初始化不同的片段。我还创建了一个新的ViewPager,并将其定义为第一个:

SectionsPagerAdapter mSectionsPagerAdapter;
OtherSectionsPagerAdapter OtherPagerAdapter;

ViewPager mViewPager;
ViewPager OtherViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    //final ActionBar actionBar = getActionBar();
    //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    OtherPagerAdapter = new OtherSectionsPagerAdapter (getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.comp);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    OtherViewPager= (ViewPager) findViewById(R.id.ScreenPager);
    OtherViewPager.setAdapter(OtherPagerAdapter);   
这是我的主要_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScreenPager"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:fadingEdge="vertical"
    app:context=".MainActivity" >
</android.support.v4.view.ViewPager>

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/comp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fadingEdge="vertical"
    app:context=".MainActivity" >
</android.support.v4.view.ViewPager></LinearLayout>

从两个ViewPager中删除这些行

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

发布崩溃后生成的日志!在getItem()函数中,通过这样做,并调整我必须匹配的节数和片段数,修复了这个问题!
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"