【发布时间】:2020-11-03 20:06:10
【问题描述】:
我在屏幕顶部有一个搜索字段(但不在工具栏中),下面我有他的 recyclerview 内容:
<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 并且效果很好,但现在我到处使用 recyclerview 和 recycleradapter,不知道为什么该字段被隐藏。找了很多recyclerview的搜索解决方案,那个字段是放在工具栏中的,但是我不想要,searchview需要在它下面。
【问题讨论】:
-
layout文件有问题?在RelativeLayout中你需要使用layout_below等属性吗? -
哦,我花了几个小时用代码创造了奇迹,真的,layout_below 解决了我的问题。我不敢相信。谢谢你,如果你把它写成答案,我会接受它!
-
很高兴它解决了这个问题,我把它作为答案
标签: android android-recyclerview searchview