It's not a code problem, but I have a question about the search button in the toolbar. This is my main xml code.
<android.support.design.widget.AppBarLayout
android:id="@+id/mainActivity_appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/mainActivity_toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@color/white">
</android.support.v7.widget.Toolbar>
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/toolBar_searchView"
style="@style/MaterialSearchViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<com.app.jiwon.tekken7_manual.Custom.CustomViewPager
android:id="@+id/mainActivity_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/mainActivity_coordinatorLayout"
android:layout_below="@+id/mainActivity_appBarLayout" />
<View
android:id="@+id/mainActivity_navShdow"
android:layout_width="match_parent"
android:layout_height="@dimen/item_select_recyclerView_cardView_shadow"
android:layout_above="@+id/mainActivity_coordinatorLayout"
android:background="@drawable/bottom_nav_shadow" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/mainActivity_coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/mainActivity_tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:selectedBackgroundVisible="false">
</com.aurelhubert.ahbottomnavigation.AHBottomNavigation>
</android.support.design.widget.CoordinatorLayout>
As you can see from the code, there is a toolbar in the main and it is a screen consisting of four fragments in the viewpager. When I try to implement the search button here, it seems that the performance will be severely degraded to announce the filter function on the broadcast, so I wonder if there is any other good way to communicate with the Recycler view of the main toolbar and the fragment!
android java
There can be many ways. Let me give you an example as a basis when a Submit Callback in SearchView occurs.
Write a method for handling callback in the Fragment and call it.
public class MainActivity extensions AppCompatActivity {
private MaterialSearchView searchView;
private ViewPager pager;
private TestAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView = findViewById(R.id.search_view);
pager = findViewById(R.id.viewPager);
adapter = new TestAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
((TestFragment) adapter.getItem(pager.getCurrentItem())).handleSubmittedQuery(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
return true;
}
static class TestAdapter extends FragmentStatePagerAdapter {
private List<TestFragment> fragmentList = new ArrayList<>();
public TestAdapter(FragmentManager fm) {
super(fm);
for (int i = 0; i < 5; i++) {
fragmentList.add(new TestFragment());
}
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
public static class TestFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
public void handleSubmittedQuery(String query) {
//do something (list update etc..)
}
}
}
getActivity() in each fragment.You can also think of a way to get a reference from the SearchView of Activity with findViewById(R.id.search_view)
and handle it directly, but personally, I think this method is better.
© 2024 OneMinuteCode. All rights reserved.