一段时间后导航抽屉,在屏幕的任何地方保持触摸关闭
Navigation Drawer after a while , keep closing on touch everywhere of screen
问题是在使用应用程序一段时间后,如果用户打开导航抽屉并触摸任何地方,甚至是导航抽屉的项目,导航抽屉保持关闭并且触摸事件发生在随机时间,所以我无法使用调试。谁能帮我解决这个问题,我不知道为什么会这样
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
try {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_share) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
// change the type of data you need to share,
//for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "https://cafebazaar.ir/app/com.example.mj.darkchat/?l=fa");
startActivity(Intent.createChooser(intent, "Share"));
} catch (Exception e) {
}
} else if (id == R.id.action_settings) {
Intent setting = new Intent(MainActivity.this, Setting.class);
startActivity(setting);
} else if (id == R.id.action_change_story) {
onAllStorysRequested();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} catch (Exception e) {
Log.e("optionSelected", "problem is option one : " + e);
return true;
}
}
你好像进入了一个状态 int id = item.getItemId(); returns 一个意外的值,您对 id 值的逻辑检查失败,因此无论您单击何处,代码都会落入关闭抽屉代码。可能由于某种原因,所有 MenuItem 的 id 字段最终都为零。
看起来有些菜单点击会启动一个新的 activity,可能当您在启动新的 activity 或后台运行应用程序并返回后返回时,MenuItem 对象会丢失其 itemId 值。
建议您添加一些调试日志记录或设置断点并监控您返回的 ID 值。还要添加最后一个 else 子句,以便您可以看到意外的 menuItem 值。即:
...
} else if (id == R.id.action_change_story) {
onAllStorysRequested();
} else {
Log.e("optionSelected", "Unexpected MenuItem.id value : " + id);
}
经过多次测试,我意识到为什么会发生这个问题,问题出在我的 xml 我在 NavigationView 之后设置了一个视图,然后首先将该视图的可见性设置为 gone 运行 一切正常,但是当我设置该视图的可见性可见代码以通知用户某些事情并且在用户通知后再次设置该视图的可见性代码然后 NavigationView 变得疯狂并且触摸事件不响应
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
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"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</android.support.v4.widget.DrawerLayout>
为了修复,我像这样更改了代码并在 NavigationView 之前设置了该视图,我不知道为什么但问题已解决。
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<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"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
问题是在使用应用程序一段时间后,如果用户打开导航抽屉并触摸任何地方,甚至是导航抽屉的项目,导航抽屉保持关闭并且触摸事件发生在随机时间,所以我无法使用调试。谁能帮我解决这个问题,我不知道为什么会这样
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
try {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_share) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
// change the type of data you need to share,
//for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "https://cafebazaar.ir/app/com.example.mj.darkchat/?l=fa");
startActivity(Intent.createChooser(intent, "Share"));
} catch (Exception e) {
}
} else if (id == R.id.action_settings) {
Intent setting = new Intent(MainActivity.this, Setting.class);
startActivity(setting);
} else if (id == R.id.action_change_story) {
onAllStorysRequested();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} catch (Exception e) {
Log.e("optionSelected", "problem is option one : " + e);
return true;
}
}
你好像进入了一个状态 int id = item.getItemId(); returns 一个意外的值,您对 id 值的逻辑检查失败,因此无论您单击何处,代码都会落入关闭抽屉代码。可能由于某种原因,所有 MenuItem 的 id 字段最终都为零。
看起来有些菜单点击会启动一个新的 activity,可能当您在启动新的 activity 或后台运行应用程序并返回后返回时,MenuItem 对象会丢失其 itemId 值。
建议您添加一些调试日志记录或设置断点并监控您返回的 ID 值。还要添加最后一个 else 子句,以便您可以看到意外的 menuItem 值。即:
...
} else if (id == R.id.action_change_story) {
onAllStorysRequested();
} else {
Log.e("optionSelected", "Unexpected MenuItem.id value : " + id);
}
经过多次测试,我意识到为什么会发生这个问题,问题出在我的 xml 我在 NavigationView 之后设置了一个视图,然后首先将该视图的可见性设置为 gone 运行 一切正常,但是当我设置该视图的可见性可见代码以通知用户某些事情并且在用户通知后再次设置该视图的可见性代码然后 NavigationView 变得疯狂并且触摸事件不响应
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
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"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</android.support.v4.widget.DrawerLayout>
为了修复,我像这样更改了代码并在 NavigationView 之前设置了该视图,我不知道为什么但问题已解决。
<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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/correct_answer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
<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"
android:background="@color/bg_drower"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>