【发布时间】:2020-05-18 19:00:39
【问题描述】:
我在执行搜索视图时遇到问题。我的 searchview 使用 onQuetyTextChanged,每次更改文本时显示搜索结果。我希望在删除文本输入时数据恢复到原始状态。我使用此代码transaction.replace(R.id.frame_container, moviesFragment); 来恢复片段,但搜索结果片段仍位于原始片段后面,如下图所示。如何删除搜索结果的片段?
Image after performing search. 这是我的搜索查看代码
final SearchView searchView = (SearchView) (menu.findItem(R.id.search)).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("query", newText);
SearchMovieFragment fragment = new SearchMovieFragment();
fragment.setArguments(bundle);
transaction.replace(R.id.frame_container, fragment);
transaction.commit();
} else {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MoviesFragment moviesFragment = new MoviesFragment();
transaction.replace(R.id.frame_container, moviesFragment);
transaction.commit();
}
return false;
}
});
还有我的activity_main.xml
<LinearLayout 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:orientation="vertical"
android:fitsSystemWindows="true"
android:background="@android:color/white"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/color_primary_gradient"
app:tabTextColor="#D4AF37"/>
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
我该如何解决这个问题?
【问题讨论】:
标签: android xml android-activity fragment