如何动态地在选项卡布局中的片段之一上的操作栏上添加“搜索视图"? [英] How to add Search View on Action bar on one of the fragments in tab layout dynamically?

查看:85
本文介绍了如何动态地在选项卡布局中的片段之一上的操作栏上添加“搜索视图"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您使用的是使用TabLaout& ViewPager进行家庭活动的android应用,请为我提供更好的解决方案

Please provide me a better solution if you have I am working on android app in which on home Activity by using TabLaout&ViewPager i have added 5 tabs on below

  1. 首页
  2. 社交
  3. 通知
  4. 搜索
  5. 链接

我已经为MainActivity设置了值/样式,但已将自定义操作栏标签放置在search.xml中.我知道如何在活动的操作栏上添加SearchView,但是我遇到了使用片段的问题.我的目标只是在用户将在搜索"选项卡上显示时在操作栏上显示搜索视图小部件.在其他标签上,我不想显示.我添加了更多 标签和setHasOptionsMenu(true)中清单文件中的<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>; 在搜索片段中.但是操作栏显示在搜索屏幕上,而没有搜索查看小部件",请纠正我做错的地方.

I have set for MainActivity in values/style but i have placed custom action bar tag in search.xml. I know how to add SearchView on action bar in activity but i am facing issues using fragments. My goal is just to show search view widget on action bar when user will on "Search" tab. On other tabs i don't want to show. Further more I have added <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> in Manifest file in tag and setHasOptionsMenu(true); in Search Fragment. But action bar is showing on search screen without search View Widget, please correct me where i am doing wrong.

推荐答案

您只需要在searchFragment中添加SearchView:

You have to add the SearchView only in the searchFragment:

searchFragment类:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_news, container, false);
...        
  setHasOptionsMenu(true);
..... 
return rootView ;}
 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_search, menu);
        try {
            // Associate searchable configuration with the SearchView
            SearchManager searchManager =
                    (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.action_search).getActionView();
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getActivity().getComponentName()));
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                   // do your search
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
           // do your search on change or save the last string or... 
                    return false;
                }
            });


        }catch(Exception e){e.printStackTrace();}
    }

menu_search 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">
    <item android:id="@+id/action_search"
        android:icon="@drawable/action_search_btn"
        android:title="Search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

这篇关于如何动态地在选项卡布局中的片段之一上的操作栏上添加“搜索视图"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆