我在屏幕顶部(但不在工具栏中)有一个搜索字段,下面我有回收器查看内容:
<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 个字符时,搜索结果将显示从服务器链接(我正在使用改造):
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并且它工作正常,但现在我到处都在使用回收器视图和回收器适配器,不知道为什么该字段被隐藏。我找到了很多带有回收器视图的搜索解决方案,该字段放置在工具栏中,但我不想要它,搜索视图需要位于其下方。
布局文件的问题。RelativeLayout
需要使用layout_below
作为属性。