2

我的应用程序有问题,我目前正在实现选项卡导航,并且我想要一个导航抽屉。问题是我真的不知道如何正确地做到这一点,我使用了developers.android.com 上给出的示例,但它不适用于ViewPager。如果我在另一个应用程序中使用它,它会很好用。

我的 LogCat 是这样的:

11-13 15:47:38.096: E/FragmentManager(15675): No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f34380 #0 id=0x7f070043}
11-13 15:47:38.096: E/FragmentManager(15675): Activity state:
11-13 15:47:38.116: E/FragmentManager(15675):   Local FragmentActivity 41ed6cd0 State:
11-13 15:47:38.116: E/FragmentManager(15675):     mCreated=falsemResumed=false mStopped=false mReallyStopped=false
11-13 15:47:38.126: E/FragmentManager(15675):     mLoadersStarted=false
11-13 15:47:38.126: E/FragmentManager(15675):   FragmentManager misc state:
11-13 15:47:38.126: E/FragmentManager(15675):     mActivity=com.videotrafico.MainActivity@41ed6cd0
11-13 15:47:38.126: E/FragmentManager(15675):     mContainer=android.support.v4.app.FragmentActivity$2@41ed8638
11-13 15:47:38.126: E/FragmentManager(15675):     mCurState=1 mStateSaved=false mDestroyed=false
11-13 15:47:38.126: E/FragmentManager(15675):   View Hierarchy:
11-13 15:47:38.126: E/FragmentManager(15675):     com.android.internal.policy.impl.PhoneWindow$DecorView{41ed91c8 V.E..... ... 0,0-0,0}
11-13 15:47:38.126: E/FragmentManager(15675):       android.widget.LinearLayout{41ed99d0 V.E..... ... 0,0-0,0}
11-13 15:47:38.126: E/FragmentManager(15675):         com.android.internal.widget.ActionBarContainer{41eda5a0 V.ED.... ... 0,0-0,0 #102035a android:id/action_bar_container}
11-13 15:47:38.126: E/FragmentManager(15675):           com.android.internal.widget.ActionBarView{41edb4f8 V.E..... ... 0,0-0,0 #102035b android:id/action_bar}
11-13 15:47:38.136: E/FragmentManager(15675):             android.widget.LinearLayout{41edccc8 VFE...C. ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):               com.android.internal.widget.ActionBarView$HomeView{41edfa78 V.E..... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.ImageView{41edfe58 V.ED.... ... 0,0-0,0 #1020246 android:id/up}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.ImageView{41ee0430 V.ED.... ... 0,0-0,0 #102002c android:id/home}
11-13 15:47:38.136: E/FragmentManager(15675):               android.widget.LinearLayout{41ee1c38 G.E..... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.ImageView{41ee1fa0 G.ED.... ... 0,0-0,0 #1020246 android:id/up}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.LinearLayout{41ee22a8 V.E..... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                   android.widget.TextView{41ee2618 V.ED.... ... 0,0-0,0 #102024d android:id/action_bar_title}
11-13 15:47:38.136: E/FragmentManager(15675):                   android.widget.TextView{41ee38e0 G.ED.... ... 0,0-0,0 #102024e android:id/action_bar_subtitle}
11-13 15:47:38.136: E/FragmentManager(15675):           com.android.internal.widget.ActionBarContextView{41ee4a38 G.E..... ... 0,0-0,0 #102035c android:id/action_context_bar}
11-13 15:47:38.136: E/FragmentManager(15675):           com.android.internal.widget.ScrollingTabContainerView{41ef9da0 VFED.... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):             android.widget.LinearLayout{41efa910 V.ED.... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):               com.android.internal.widget.ScrollingTabContainerView$TabView{41efb180 VFE...C. .S. 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.TextView{41efcbe0 V.ED.... .S. 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):               com.android.internal.widget.ScrollingTabContainerView$TabView{41efdad0 VFE...C. ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.TextView{41efe368 V.ED.... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):               com.android.internal.widget.ScrollingTabContainerView$TabView{41efea98 VFE...C. ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):                 android.widget.TextView{41eff330 V.ED.... ... 0,0-0,0}
11-13 15:47:38.136: E/FragmentManager(15675):         android.widget.FrameLayout{41ee56f0 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
11-13 15:47:38.136: E/FragmentManager(15675):           android.support.v4.view.ViewPager{41ee85d8 VFED.... ... 0,0-0,0 #7f07003c app:id/pager}
11-13 15:47:38.146: E/FragmentManager(15675):             android.support.v4.widget.DrawerLayout{41ef0ee8 VFE..... ... 0,0-0,0 #7f07003d app:id/drawer_layout}
11-13 15:47:38.146: E/FragmentManager(15675):               android.widget.ListView{41ef5958 VFED.VC. ... 0,0-0,0 #7f07003e app:id/left_drawer}
11-13 15:47:38.146: E/FragmentManager(15675):         com.android.internal.widget.ActionBarContainer{41ee5de8 G.ED.... ... 0,0-0,0 #102035d android:id/split_action_bar}
11-13 15:47:38.146: D/AndroidRuntime(15675): Shutting down VM
11-13 15:47:38.146: W/dalvikvm(15675): threadid=1: thread exiting with uncaught exception (group=0x41c1a930)
11-13 15:47:38.166: E/AndroidRuntime(15675): FATAL EXCEPTION: main
11-13 15:47:38.166: E/AndroidRuntime(15675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.videotrafico/com.videotrafico.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f34380 #0 id=0x7f070043}
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.os.Looper.loop(Looper.java:137)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at dalvik.system.NativeStart.main(Native Method)
11-13 15:47:38.166: E/AndroidRuntime(15675): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f34380 #0 id=0x7f070043}
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.Activity.performStart(Activity.java:5113)
11-13 15:47:38.166: E/AndroidRuntime(15675):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
11-13 15:47:38.166: E/AndroidRuntime(15675):    ... 11 more
11-13 15:47:41.046: I/Process(15675): Sending signal. PID: 15675 SIG: 9
11-13 15:47:41.146: W/Trace(15816): error opening trace file: No such file or directory (2)
11-13 15:47:41.486: E/FragmentManager(15816): No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f21140 #0 id=0x7f070043}
11-13 15:47:41.486: E/FragmentManager(15816): Activity state:
11-13 15:47:41.486: E/FragmentManager(15816):   Local FragmentActivity 41ec3720 State:
11-13 15:47:41.486: E/FragmentManager(15816):     mCreated=falsemResumed=false mStopped=false mReallyStopped=false
11-13 15:47:41.486: E/FragmentManager(15816):     mLoadersStarted=false
11-13 15:47:41.486: E/FragmentManager(15816):   FragmentManager misc state:
11-13 15:47:41.486: E/FragmentManager(15816):     mActivity=com.videotrafico.MainActivity@41ec3720
11-13 15:47:41.486: E/FragmentManager(15816):     mContainer=android.support.v4.app.FragmentActivity$2@41ec5160
11-13 15:47:41.486: E/FragmentManager(15816):     mCurState=1 mStateSaved=false mDestroyed=false
11-13 15:47:41.486: E/FragmentManager(15816):   View Hierarchy:
11-13 15:47:41.486: E/FragmentManager(15816):     com.android.internal.policy.impl.PhoneWindow$DecorView{41ec5f60 V.E..... ... 0,0-0,0}
11-13 15:47:41.486: E/FragmentManager(15816):       android.widget.LinearLayout{41ec76c8 V.E..... ... 0,0-0,0}
11-13 15:47:41.486: E/FragmentManager(15816):         com.android.internal.widget.ActionBarContainer{41ec8298 V.ED.... ... 0,0-0,0 #102035a android:id/action_bar_container}
11-13 15:47:41.486: E/FragmentManager(15816):           com.android.internal.widget.ActionBarView{41ec9d58 V.E..... ... 0,0-0,0 #102035b android:id/action_bar}
11-13 15:47:41.496: E/FragmentManager(15816):             android.widget.LinearLayout{41ecb528 VFE...C. ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):               com.android.internal.widget.ActionBarView$HomeView{41ece1c8 V.E..... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.ImageView{41ece708 V.ED.... ... 0,0-0,0 #1020246 android:id/up}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.ImageView{41ecece0 V.ED.... ... 0,0-0,0 #102002c android:id/home}
11-13 15:47:41.496: E/FragmentManager(15816):               android.widget.LinearLayout{41ed05f8 G.E..... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.ImageView{41ed0960 G.ED.... ... 0,0-0,0 #1020246 android:id/up}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.LinearLayout{41ed0c68 V.E..... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                   android.widget.TextView{41ed1100 V.ED.... ... 0,0-0,0 #102024d android:id/action_bar_title}
11-13 15:47:41.496: E/FragmentManager(15816):                   android.widget.TextView{41ed2468 G.ED.... ... 0,0-0,0 #102024e android:id/action_bar_subtitle}
11-13 15:47:41.496: E/FragmentManager(15816):           com.android.internal.widget.ActionBarContextView{41ed35c0 G.E..... ... 0,0-0,0 #102035c android:id/action_context_bar}
11-13 15:47:41.496: E/FragmentManager(15816):           com.android.internal.widget.ScrollingTabContainerView{41ee9138 VFED.... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):             android.widget.LinearLayout{41ee9ca8 V.ED.... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):               com.android.internal.widget.ScrollingTabContainerView$TabView{41eea518 VFE...C. .S. 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.TextView{41eebf78 V.ED.... .S. 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):               com.android.internal.widget.ScrollingTabContainerView$TabView{41eece68 VFE...C. ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.TextView{41eed700 V.ED.... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):               com.android.internal.widget.ScrollingTabContainerView$TabView{41eede30 VFE...C. ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):                 android.widget.TextView{41eee6c8 V.ED.... ... 0,0-0,0}
11-13 15:47:41.496: E/FragmentManager(15816):         android.widget.FrameLayout{41ed4260 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
11-13 15:47:41.496: E/FragmentManager(15816):           android.support.v4.view.ViewPager{41ed7910 VFED.... ... 0,0-0,0 #7f07003c app:id/pager}
11-13 15:47:41.496: E/FragmentManager(15816):             android.support.v4.widget.DrawerLayout{41ee0280 VFE..... ... 0,0-0,0 #7f07003d app:id/drawer_layout}
11-13 15:47:41.496: E/FragmentManager(15816):               android.widget.ListView{41ee4cf0 VFED.VC. ... 0,0-0,0 #7f07003e app:id/left_drawer}
11-13 15:47:41.496: E/FragmentManager(15816):         com.android.internal.widget.ActionBarContainer{41ed4958 G.ED.... ... 0,0-0,0 #102035d android:id/split_action_bar}
11-13 15:47:41.506: D/AndroidRuntime(15816): Shutting down VM
11-13 15:47:41.506: W/dalvikvm(15816): threadid=1: thread exiting with uncaught exception (group=0x41c1a930)
11-13 15:47:41.506: E/AndroidRuntime(15816): FATAL EXCEPTION: main
11-13 15:47:41.506: E/AndroidRuntime(15816): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.videotrafico/com.videotrafico.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f21140 #0 id=0x7f070043}
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.os.Looper.loop(Looper.java:137)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at dalvik.system.NativeStart.main(Native Method)
11-13 15:47:41.506: E/AndroidRuntime(15816): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f070043 (com.videotrafico:id/content_frame) for fragment PlanetFragment{41f21140 #0 id=0x7f070043}
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.Activity.performStart(Activity.java:5113)
11-13 15:47:41.506: E/AndroidRuntime(15816):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
11-13 15:47:41.506: E/AndroidRuntime(15816):    ... 11 more

布局的代码是这样的:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#069"/>
</android.support.v4.widget.DrawerLayout>
</android.support.v4.view.ViewPager>

我的 Java 对于 MainActivity 是这样的:

mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

     // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
       getActionBar().setDisplayHomeAsUpEnabled(true);
       getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                //getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                //getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
   }

/* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        //mTitle = title;
        //getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    /**
     * Fragment that appears in the "content_frame", shows a planet
     */
    public static class PlanetFragment extends Fragment {
        public static final String ARG_PLANET_NUMBER = "planet_number";

        public PlanetFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt(ARG_PLANET_NUMBER);
            String secciones = getResources().getStringArray(R.array.planets_array)[i];

            if (i == 6){
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("Cerrar tu sesiÛn eliminar· datos locales del dispositivo y te desconectar· del punto net.")
                       .setCancelable(false)
                       .setPositiveButton("De acuerdo", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               new asynclogout().execute(usuarioID);
                           }
                       })
                       .setNegativeButton("No gracias", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show();
            }
            if (i == 5){
                Intent intent=new Intent(getActivity().getApplication(),PrefsActivity.class);
                startActivity(intent);

            }
            if (i == 0){

            }
}

我希望你能帮我解决这个问题。

谢谢!

编辑:我发现了问题,但我不知道如何替换 FrameLayout:

private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

另一个应用程序在主布局中有这个:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff">

我的之前已经发布了代码。

4

2 回答 2

5

我回答自己,我已将布局更改为:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp" >

            <android.support.v4.view.PagerTabStrip
                android:id="@+id/tabstrip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </android.support.v4.view.ViewPager>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#069"/>
</android.support.v4.widget.DrawerLayout>

在 MainActivity 上改变了这个:

private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

对此:

private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.pager, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
于 2013-11-13T20:59:34.843 回答
1

尝试检查一下:https ://github.com/Balaji-K13/Navigation-drawer-page-sliding-tab-strip

此解决方案使用“嵌套片段”,它允许片段中的 viewpager。
(嵌套片段:https ://github.com/marcoRS/nested-fragments )

所以可以先用NavigationDrawer,把ViewPager放到PlaceHolderFragment中。

该项目还为您提供了一个有用的示例和资源。

于 2014-03-27T09:42:12.690 回答