我的工具栏中有一个SearchView,用于CardViews的RecyclerView列表,它们保存在Room数据库中。我通过SearchView用搜索文本过滤列表,然后左键滑动其中一张返回的卡,将其从数据库中删除。UI将正确更新,以显示任何剩余的已筛选CardViews。
问题是当我点击";x〃;,我在搜索EditText行右侧设置的关闭按钮。左滑动删除过滤卡后,过滤列表正确消失,用户界面正确更新数据库中剩余CardViews的新列表。然而,在UI的更新期间,搜索EditText行和;X〃;关闭按钮被破坏,工具栏返回显示我设置的SearchView的默认状态,即显示图标化的搜索图标。
然而,我想保持搜索EditText行打开,清除搜索文本并保留"编辑文本";x〃;UI更新后,EdixtText行右侧的关闭按钮。
我尝试将以下代码片段添加到mSearchView、searchItem和mSearchEditText变量中,以尝试在UI更新后重新打开搜索EditText行,但没有成功:
- .setIconified(false(
- .performClick((
- .exexpandActionView((
- .requestFocus((
我还使用ExecutiorService将Room数据库的ViewModel更新放在后台线程中,然后在后台线程完成后,尝试更新UI并清除主线程上的搜索EditText行。但图标化的搜索图标仍然被返回。
那么我错过了什么?我该如何实现?
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainactiv_menu, menu);
searchItem = menu.findItem(R.id.action_search);
menu.findItem(R.id.action_search).setVisible(false);
if (cardsAdapter != null && cardsAdapter.getItemCount() > 0) {
menu.findItem(R.id.action_search).setVisible(true);
}
SearchManager searchManager = (SearchManager)
MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnQueryTextFocusChangeListener(new SearchViewFocusListener(searchItem));
if (mSearchView != null) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchEditText = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
mSearchEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mSearchEditText.setMaxLines(1);
ImageView closeButton = mSearchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(v -> {
// gets the data from the Room database.
mQuickcardViewModel.loadFullList();
// reload the database data in to the RecyclerView Adapter. This destroys
// the search EditText line for the SearchView and also destroys the "x" close
// button and the search icon re-appears.
cardsAdapter.reloadFullList(mQuickcards);
// Clear the current search text so the member can enter different search text.
// This is not working as the UI shows the search icon as iconified.
mSearchView.setQuery("",false);
});
mainactiv_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
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"
tools:context=".MainActivity">
<item android:id="@+id/action_search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:orderInCategory="1"
app:showAsAction="always|collapseActionView" />
<item android:id="@+id/action_new"
android:title="@string/action_new"
android:icon="@drawable/ic_action_new"
tools:targetApi="o"
android:tooltipText="Add a quickcard"
android:orderInCategory="2"
app:showAsAction="always" />
<item android:id="@+id/action_overflow"
android:title="@string/action_overflow"
android:icon="@drawable/ic_action_overflow"
android:orderInCategory="3"
app:showAsAction="always" >
<menu>
<item
android:id="@+id/number_of_cards"
android:title=""
app:showAsAction="always" />
</menu>
</item>
</menu>
代码:主活动.java公共布尔值onCreateOptionsMenu(菜单菜单({
getMenuInflater().inflate(R.menu.mainactiv_menu, menu);
searchItem = menu.findItem(R.id.action_search);
menu.findItem(R.id.action_search).setVisible(false);
if (cardsAdapter != null && cardsAdapter.getItemCount() > 0) {
menu.findItem(R.id.action_search).setVisible(true);
}
SearchManager searchManager = (SearchManager)
MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
if (searchItem != null) {
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnQueryTextFocusChangeListener(new SearchViewFocusListener(searchItem));
if (mSearchView != null) {
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchEditText = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
mSearchEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
mSearchEditText.setMaxLines(1);
ImageView closeButton = mSearchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(v -> {
// gets the data from the Room database.
mQuickcardViewModel.loadFullList();
// reload the database data in to the RecyclerView Adapter. This destroys
// the search EditText line for the SearchView and also destroys the "x" close
// button and the search icon re-appears.
cardsAdapter.reloadFullList(mQuickcards);
// Clear the current search text so the member can enter different search text.
// This is not working as the UI shows the search icon as iconified.
mSearchView.setQuery("",false);
});