点击Navigation Drawer Fragment点击Main Content Fragment

时间:2016-01-01 18:27:28

标签: android android-fragments navigation-drawer

获取有线输出

当我点击导航抽屉的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

2 个答案:

答案 0 :(得分:3)

确保左侧抽屉布局中的所有内容都是可点击的。否则,点击将传递到基础视图,在本例中将传递给您的主要内容。您可以通过将OnClickListener设置为FragmentDrawer的根视图来执行此操作:

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

答案 1 :(得分:1)

在inDrawerOpen方法中,您可以使用yourcontentlayout.setEnabled(false)和onDrawerClosed方法yourcontentlayout.setEnabled(true)

希望这有帮助!