最近,我一直在尝试通过实现多个滑动视图来为我的应用程序创建高级外观。
让我解释一下“多次滑动视图”的含义:
请原谅我糟糕的绘画技巧。
如您所见,我只是试图将滑动视图堆叠在一起。当然,每个滑动视图都包含不同的片段。
我成功地创建了一个滑动视图,但这就是我所做的一切。为了创建一个滑动视图,我创建了从 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)