最近,我一直試圖通過對其執行多個滑動視圖來爲我的應用創建高級外觀。Android多次滑動視圖
讓我解釋一下我所說的「多刷卡視圖」的意思是:
並請原諒我差的油漆技能。
正如你所看到的,我只是簡單地將swipe-views堆疊在一起。每個滑動視圖都包含不同的片段。
我成功地創建了一個滑動視圖,但這是我設法做的。 要創建一個滑動 - 視圖我創建的從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)
發佈您的Logcat產生後崩潰 – Rahul