其他分享
首页 > 其他分享> > android-单击导航抽屉片段,然后单击主要内容片段

android-单击导航抽屉片段,然后单击主要内容片段

作者:互联网

获取有线输出

当我单击导航抽屉的TextView时,单击返回到后面的片段(主要内容片段)

请有人帮助我做错了什么.

布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The toolbar -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:title="@string/app_name" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <!-- The main content view -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/content_main" />

    </LinearLayout>

    <!-- The navigation drawer -->
    <fragment
        android:id="@+id/left_drawer"
        android:name="app.compiler.fragment.FragmentDrawer"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_drawer"
        />
</android.support.v4.widget.DrawerLayout>

Java代码

public class ActivityMain extends AppCompatActivity {

    DrawerLayout mDrawerLayout;


    ActionBarDrawerToggle mDrawerToggle;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ide);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);


            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.app_name, R.string.app_name)
            {

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                }

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);


            if (savedInstanceState == null) {

                FragmentMain fragmentMain = new FragmentMain();

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.add(R.id.container, fragmentMain);
                transaction.commit();

            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = new MenuInflater(this);
            inflater.inflate(R.menu.menu_main,menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public void onBackPressed() {
            if(mDrawerLayout.isDrawerOpen(Gravity.START | Gravity.LEFT)){
                mDrawerLayout.closeDrawers();
                return;
            }
            super.onBackPressed();
        }
    }

检查以下gif图片中的代码输出
output of code

解决方法:

确保左抽屉布局中的所有内容均可点击.否则,点击将被传递到基础视图,在这种情况下,将传递到您的主要内容.您可以通过将OnClickListener设置为FragmentDrawer的rootview来实现:

myFragmentInsideTheDrawer.getView().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View pView) {
                // do nothing here, just intercept click-events
            }
        });

标签:android-fragments,navigation-drawer,android
来源: https://codeday.me/bug/20191027/1945114.html