导航抽屉在单击事件时关闭

时间:2019-05-31 20:27:24

标签: java android


我无法单击任何菜单项,也无法从右向左滑动以关闭抽屉,尽管每当打开抽屉并单击屏幕时,抽屉都会立即关闭。

我已经在堆栈溢出和Github上尝试过所有发布给类似问题的答案:

 public class BottomActivity extends AppCompatActivity
    implements GestureOverlayView.OnGesturePerformedListener {

RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;
DrawerLayout drawer;



@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_bottom );
    new GestureOverlayView( this );



    Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
    getSupportActionBar().setCustomView( R.layout.abs_layout_home );


    radioGroup = findViewById( R.id.radioGroup );
    Rd1 = findViewById( R.id.radioButton );
    Rd2 = findViewById( R.id.radioButton2 );
    Rd3 = findViewById( R.id.radioButton3 );
    Rd4 = findViewById( R.id.radioButton4 );
    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (Rd1.isChecked()) {
                Intent intent = new Intent( getApplicationContext(), BottomActivity.class );
                startActivity( intent );
            }
            if (Rd2.isChecked()) {
                Intent intent1 = new Intent( getApplicationContext(), DashBoard.class );
                startActivity( intent1 );
            }
            if (Rd3.isChecked()) {
                Intent intent2 = new Intent( getApplicationContext(), SettingsActivity.class );
                startActivity( intent2 );
            } else {
                if (Rd4.isChecked()) {
                    Intent intent3 = new Intent( getApplicationContext(), Messages.class );
                    startActivity( intent3 );
                }
            }
        }
    } );









}



@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen( GravityCompat.START )) {
        drawer.closeDrawer( GravityCompat.START );
    } else {
        super.onBackPressed();
    }
}

public void onDrawerSlide() {
    DrawerLayout drawerLayout = findViewById( R.id.DrawerLayout);
    drawerLayout.addDrawerListener(
            new DrawerLayout.DrawerListener() {
                @Override
                public void onDrawerSlide(@NonNull View view, float v) {

                }

                @Override
                public void onDrawerOpened(@NonNull View view) {

                }

                @Override
                public void onDrawerClosed(@NonNull View view) {

                }

                @Override
                public void onDrawerStateChanged(int i) {

                }
            }
    );
}



@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {

}






@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.nav_profile:
            startActivity( new Intent(BottomActivity.this, ProfileActivity.class));
            return true;
        case R.id.nav_settings:
            startActivity( new Intent( BottomActivity.this, SettingsActivity.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

这是我的导航抽屉XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:openNavigationView="start"
android:fitsSystemWindows="true">


<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
    app:expandedTitleGravity="top"
    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:minHeight="?attr/actionBarSize"
    app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>

<include
    layout="@layout/app_bar_nav_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_nav_drawer"
    app:menu="@menu/activity_nav_drawer_drawer" />

这是我的活动XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:elevation="10dp"
android:foregroundGravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".BottomActivity"
tools:targetApi="lollipop"
app:layout_collapseMode="parallax">


    <include
    android:id="@+id/include"
    layout="@layout/activity_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>



    <RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/colorPrimary"
    android:gravity="bottom"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">


     <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:buttonTint="@color/iconColor"
        android:checked="true"
        android:drawableTop="@drawable/ic_home_black_24dp"
        android:padding="10dp"
        android:tag="1"
        android:text="@string/home"
        android:textAlignment="center"
        android:textColor="@color/color"
        android:textSize="12sp"/>

     <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:buttonTint="@color/uiconColor"
        android:drawableTop="@drawable/ic_notifications_black_24dp"
        android:padding="10dp"
        android:text="@string/notifications"
        android:textAlignment="center"
        android:textColor="@color/color"
        android:textSize="12sp"/>

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:buttonTint="@color/uiconColor"
        android:drawableTop="@drawable/ic_people_icon"
        android:padding="10dp"
        android:text="@string/People"
        android:textAlignment="center"
        android:textColor="@color/color"
        android:textSize="12sp"/>

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:button="@null"
        android:buttonTint="@color/uiconcolor"
        android:drawableTop="@drawable/ic_msg_icon"
        android:padding="10dp"
        android:text="@string/Messages"
        android:textAlignment="center"
        android:textColor="@color/color"
        android:textSize="12sp"/>


</RadioGroup>


</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

BottomActivity 中将其添加到您的 onCreate 中:

    NavigationView nav_view;

    nav_view = findViewById(R.id.nav_view);   //select nav_view from activity_drawer_layout

    nav_view.setNavigationItemSelectedListener(this);

activity_drawer_layout.xml 中,将 NavigationView 放在 DrawerLayout 的结束标记上方。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DrawerLayout"
android:id="@+id/DrawerLayout">

<include
    android:id="@+id/include"
    layout="@layout/app_bar_nav_drawer"
    android:layout_width="0dp"
    android:layout_height="0dp" />
<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="0dp"
    android:layout_height="673dp"
    android:padding="16dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/age"/>
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:layout_constraintBottom_toBottomOf="@+id/nav_header" />

<include
    android:id="@+id/include"
    layout="@layout/app_bar_nav_drawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layout_gravity="start"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    app:headerLayout="@layout/nav_header_nav_drawer"
    app:menu="@menu/activity_nav_drawer_drawer" />


</android.support.v4.widget.DrawerLayout>