简体   繁体   English

带有滑动标签的Android搜索界面

[英]Search interface with swipe tabs android

I need some help to implement search ability in different swipe tabs. 我需要一些帮助以在不同的滑动选项卡中实现搜索功能。 I have an Activity A which is also the main activity for my search interface (search bar at the top). 我有一个Activity A,这也是我的搜索界面的主要活动(顶部的搜索栏)。 This activity should pass the search string to respective fragments based on the tabs, All, Organizations, and persons. 此活动应基于选项卡,全部,组织和个人将搜索字符串传递到各个片段。

I am not able to figure out how can I pass search string to the respective fragments. 我无法弄清楚如何将搜索字符串传递给各个片段。 Does anyone have any suggestions or working code which i can refer to ? 有没有人可以提出任何建议或工作代码?

Thanks 谢谢

Please find the attached snapshot. 请找到所附的快照。

轻扫

In your Activity , declare a public variable 在您的Activity ,声明一个公共变量

public String search;  

In the onCreateOptionsMenu() of your Activity , add 在您的ActivityonCreateOptionsMenu()中,添加

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
        .getActionView();    

SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
    public boolean onQueryTextChange(String newText) {
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        search = query;
    }
};
searchView.setOnQueryTextListener(queryTextListener);

and in your Fragment , you can access the string as Fragment ,您可以按以下方式访问字符串

String query = ((MyActivity)getActivity()).search;

Try this. 尝试这个。 This will work. 这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM