java - 搜索后导航栏不在最后位置

标签 java android android-studio navigation-drawer

我已经实现了一个 SearchBar,在我得到结果后,我想通过使用模拟器的“X”图标或“后退”键退出导航栏,但导航栏没有转到最后一个位置 我该如何解决?

应用程序问题的屏幕截图 -> https://imgur.com/gallery/V2JN7HA

一些我用来清除状态的代码

public void getText(最终 View View ) {

    final SearchView sv = view.findViewById(R.id.searchView);
    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if(query.length() >= 1){
                userInput = query;
                fetchData();

                setAdapter(view);
                sv.clearFocus();

                view.requestFocus();
                return true;
            }
            setUpView(view);
            observeDataChange();
            setAdapter(view);

            sv.clearFocus();
            return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            if(newText.length() >= 1){
                userInput = newText;
                fetchData();

                setAdapter(view);
                //sv.clearFocus();

                view.requestFocus();
                return true;
            }
            return false;
        }

    });

}

最佳答案

用下面的代码替换你的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp">


    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />


</androidx.constraintlayout.widget.ConstraintLayout>

和 fragment_dashboard.xml 由

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@color/cardview_shadow_end_color"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="8dp"
        android:textAlignment="center"
        android:textSize="0sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_of_photos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="4dp"
        android:layout_marginRight="2dp"
        android:background="@color/cardview_shadow_end_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.516"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.666" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于java - 搜索后导航栏不在最后位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491578/

相关文章:

java - Android Studio - 获取 Activity View 实例的最佳方式

java - 强制 JOptionPane 保持打开状态

java - Eclipse 生成 Javadoc 向导 : what is "Javadoc Command"?

java - Java 类的问题

java - 如何在 JavaFx 的可编辑 ComboBox 中获取输入值?

android - 如何更改画廊的 ScrollView

android - Google protobuf 在解析字符串数据时在 Android 中崩溃

android - 我如何使用数据绑定(bind)和 Dagger 2 查找错误?

android - keystore 被篡改或密码错误

android - 在现有的SDK上安装Android Studio