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

标签 java android

<小时/>

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

我几乎尝试了在 Stack Overflow 和 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" />

这是我的 Activity 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>

最佳答案

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>

关于java - 抽屉导航在单击事件时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56401363/

相关文章:

java - 调整父 JFrame 大小时调整子面板大小

java - 节俭的 Kerberos?

android - 服务不为主要 Activity 保存 SharedPreference?

android - WebView 崩溃导致整个应用程序崩溃

android - 在 RINGING 电话状态下关闭 wifi

java - 使用startActivity()时出现运行时错误

java - 为动态 Web 项目配置 JPA 提供程序

java - Android - 写入 ISO15693 标签

android - GraphView,如何显示x轴标签?

android - Dreamweaver + Phonegap 启动