Android搜索后searchview隐藏在recyclerview中

Android searchview is hidden in recyclerview after search

我在屏幕顶部(但不在工具栏中)有一个搜索字段,在下面我有他的回收视图内容:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_fragment"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#DBDBDB"
    android:orientation="vertical">

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:background="@drawable/searchfield">
    </SearchView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DBDBDB" />

</RelativeLayout>

和我的 java 代码的一部分:

setContentView(R.layout.search_layout); //this is the above XML
        movieList = new ArrayList<>();
        recyclerView = (RecyclerView)findViewById(R.id.recyclerview); // this is the recyclerview XML (thumb, title etc)
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerAdapter = new RecyclerAdapter(this, "Search", movieList);

        SearchView  search = findViewById(R.id.searchView1);
        int id = search.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = search.findViewById(id);
        textView.setTextColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
        textView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});

        int searchPlateId = search.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        View searchPlateView = search.findViewById(searchPlateId);
        if (searchPlateView != null) {searchPlateView.setBackgroundColor(Color.TRANSPARENT);}

        search.setQueryHint(getString(R.string.searchfield));
        search.onActionViewExpanded();
        search.requestFocusFromTouch();
        search.setIconifiedByDefault(false);

        recyclerView.setAdapter(recyclerAdapter);

最后,当用户输入至少 3 个字符时,服务器会显示搜索结果 link(我正在使用改造):

    public boolean onQueryTextChange(String newText) {

        if (newText.length() > 2)
        {
            movieList = new ArrayList<>();

            recyclerAdapter.notifyDataSetChanged();
            recyclerView.setVisibility(VISIBLE);
            GetData(lng, newText);

        }
        else  {recyclerView.setVisibility(INVISIBLE);}
        return false;
    }

private void GetData(String lng, String newText) {
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<List<Movie>> call = apiService.getSearch(lng, 0, newText);

    call.enqueue(new Callback<List<Movie>>() {

        @Override
        public void onResponse(@NonNull Call<List<Movie>> call, @NonNull Response<List<Movie>> response) {
            movieList = response.body();
            if (dialog != null) {
                dialog.dismiss();  dialog = null;
            }

            if (response.isSuccessful()) {
                recyclerAdapter.setMovieList(movieList);
            } else {

                showMsgSnack(getString(R.string.Nodata));
            }

        }


        @Override
        public void onFailure(@NonNull Call<List<Movie>> call, @NonNull Throwable t) {
            if (dialog != null) {
                dialog.dismiss();  dialog = null;
            }

            if(t instanceof UnknownHostException){

                showMsgSnack(getString(R.string.Network));
            }

            else if(t instanceof SocketTimeoutException){
                showMsgSnack(getString(R.string.ServerTimeout));
            }

            else {
                showMsgSnack(getString(R.string.ServerError));
            }

        }
    });
}

搜索功能运行良好,因为它在服务器端运行并且只返回正确的结果。

问题是,当用户输入至少 3 个字符时,搜索字段消失,只有结果可见,但用户无法在任何地方输入,因为搜索字段消失了。所以从一开始这个字段就在那里,只有当搜索结果出现时它才消失。

以前我用的是ListView,效果还不错,现在到处都在用recyclerview和recycleradapter,不知道为什么这个字段被隐藏了。我找了很多recyclerview的搜索解决方案,那个字段是放在toolbar里的,但是我不要,searchview要在它下面

布局文件有问题。在 RelativeLayout 中需要将 layout_below 用作 属性。