Android多次滑动视图

时间:2013-09-22 14:42:06

标签: java android android-layout android-fragments android-viewpager

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

让我通过“多次滑动视图”来解释我的意思:

Amazingly drawn demonstration of my goal

请原谅我糟糕的油漆技巧。

正如您所看到的,我只是想在彼此之下堆叠滑动视图。每个滑动视图当然包含不同的片段。

我成功创建了一个滑动视图,但这就是我设法做到的。 要创建一个swipe-View,我创建了从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);   

这是我的main_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>

如果我正在尝试运行应用程序,它会崩溃。 我做错了什么,请你指出正确的方法?

编辑: 这是崩溃后我得到的logcat:

    09-22 16:56:14.283: E/AndroidRuntime(8816): FATAL EXCEPTION: main
09-22 16:56:14.283: E/AndroidRuntime(8816): java.lang.IllegalArgumentException: Invalid section number
09-22 16:56:14.283: E/AndroidRuntime(8816):     at com.example.casio.MainActivity$compSectionsPagerAdapter.getItem(MainActivity.java:707)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.View.measure(View.java:15848)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.View.measure(View.java:15848)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.View.measure(View.java:15848)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.View.measure(View.java:15848)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.View.measure(View.java:15848)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.os.Handler.handleCallback(Handler.java:730)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.os.Looper.loop(Looper.java:137)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at java.lang.reflect.Method.invoke(Method.java:525)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 16:56:14.283: E/AndroidRuntime(8816):     at dalvik.system.NativeStart.main(Native Method)
09-22 17:58:20.469: E/AndroidRuntime(12171): FATAL EXCEPTION: main
09-22 17:58:20.469: E/AndroidRuntime(12171): java.lang.IllegalArgumentException: Invalid section number
09-22 17:58:20.469: E/AndroidRuntime(12171):    at com.example.casio.MainActivity$compSectionsPagerAdapter.getItem(MainActivity.java:707)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.View.measure(View.java:15848)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.View.measure(View.java:15848)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.View.measure(View.java:15848)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.View.measure(View.java:15848)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.View.measure(View.java:15848)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.Choreographer.doFrame(Choreographer.java:532)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.os.Handler.handleCallback(Handler.java:730)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.os.Looper.loop(Looper.java:137)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at java.lang.reflect.Method.invoke(Method.java:525)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 17:58:20.469: E/AndroidRuntime(12171):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

从两个ViewPagers中删除这些行。

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