Android 使用 Fragment 进行搜索
有人知道如何使用 Android 搜索界面 实现标准的教程或示例吗?代码>片段?换句话说,是否可以在 Fragment 中使用 SearchManager
进行标准搜索?
Does somebody know of a tutorial or an example of how to implement the standard Android search interface with Fragment
s? In other words, is it possible to put a standard search with a SearchManager
in a Fragment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
简而言之,你不能。有几个原因导致无法在
Fragment
中创建搜索界面。创建可搜索界面时,您必须在 Android 清单中指定默认的“可搜索活动”。我相信您知道,如果没有父
Activity
,Fragment
就不可能存在,因此,这种分离是不可能的。如果你已经弄清楚了#1,我想你问这个问题是希望有一些神奇的“黑客”可以完成工作。但是,文档指出,
<块引用>
当用户在搜索对话框或小部件中执行搜索时,
系统启动您的可搜索活动并向其提供搜索
使用 ACTION_SEARCH 操作在 Intent 中进行查询。您的可搜索
活动从意图的 QUERY extra 中检索查询,然后
搜索您的数据并显示结果。
负责提供搜索结果的底层内部系统需要一个
Activity
,而不是一个Fragment
;因此,实现完全独立于 Activity 的搜索界面是不可能的,因为它需要对底层系统本身进行更改。查看SearchableInfo
类,如果你不相信我:)。话虽如此,实现与您所描述的类似的目标似乎并不太困难。例如,您可能会考虑实现您的 searchable-Activity,以便它将接受
android.intent.action.SEARCH
意图并且(而不是立即在ListView
中显示结果) ,例如)会将搜索查询传递给您的Fragment
。例如,考虑以下可搜索活动:当发出搜索请求时,系统将启动您的可搜索活动,执行查询,并将结果传递给某些容器活动(基于您的
doMySearch)。然后,容器 Activity 将这些结果传递给包含的可搜索
Fragment
,结果将在其中显示。该实现需要比您可能希望的更多的工作,但我确信有一些方法可以使其更加模块化,而且看起来这可能是您能做的最好的事情。ps 如果您使用这种方法,您可能需要特别注意哪些 Activity 被添加/删除到后台堆栈。有关如何完成此操作的更多信息,请参阅此帖子。
pps 您也可能完全忘记标准搜索界面,而只是在
Fragment
中实现简单的搜索,如 中所述下面是 Raghav 的帖子。In short, you can't. There are a couple of reasons why creating a search interface within a
Fragment
is not possible.When creating a searchable interface, you must specify a default "searchable activity" in your Android manifest. As I'm sure you know, a
Fragment
cannot exist without a parentActivity
and thus, this separation is not possible.If you already figured out #1 already, I assume you asked this question in hopes that there is some magical "hack" out there that can get the job done. However, the documentation states that,
The underlying, internal system that is responsible for providing search results expects an
Activity
, not aFragment
; thus, implementing a search interface that is completely independent of anActivity
is not possible, as it would require changes to the underlying system itself. Check out the source code for theSearchableInfo
class if you don't believe me :).That being said, it doesn't seem like it would be too difficult to achieve something similar to what you are describing. For instance, you might consider implementing your searchable-Activity so that it will accept the
android.intent.action.SEARCH
intent and (instead of immediately displaying the results in aListView
, for example) will pass the search query to yourFragment
s. For instance, consider the following searchable Activity:When a search-request is made, the system will launch your searchable activity, perform the query, and will pass the results to some container Activity (based on your implementation of
doMySearch
). The container Activity will then pass these results to the contained searchableFragment
, in which the results will be displayed. The implementation requires a bit more work than what you were probably hoping for, but I'm sure there are ways that you can make it more modular, and it seems like this might be the best that you can do.p.s. If you use this approach, you might have to pay special attention to which Activitys are added/removed to the backstack. See this post for some more information on how this might be done.
p.p.s. You might also forget about the standard search interface completely and just implement a simple search within a
Fragment
as described in Raghav's post below.这是使用片段搜索内容的示例。希望它能有所帮助,这就是您正在寻找的:
Here is the example to search something using fragments. Hope it helps and this is what you are looking for:
很可能使用标准 ActionBar SearchView ActionView API 在片段中进行搜索。这也将使用 AppCompat 支持类 v7 回到 Android 2.1(API 级别 7)。
在您的片段中:
在您的菜单 XML 中
It is quite possible to search in a fragment using the standard ActionBar SearchView ActionView API. This will work back to Android 2.1 (API level 7) too using AppCompat support classes v7.
In your fragment:
In your menu XML
使用 AppCompat 支持类 v7。只需从 @Rookie 解决方案中向 @David 的解决方案添加一些内容,即可以简单的方式使其正常工作,这是我的片段代码:
MyFragment >:
我添加了
onActivityCreated
,因为没有调用setHasOptionsMenu(true);
系统不会知道这个片段需要与菜单交互。然后我删除了行
inflater.inflate(R.menu.main, menu);
因为它使菜单项加倍,因为 Activity 膨胀了一个菜单,然后 Fragment 膨胀了另一个菜单感谢 @David 和 @Rookie
Using AppCompat support classes v7. Just adding something to @David 's solution from @Rookie solution to get it work properly in a simple manner, here is my fragment code:
MyFragment:
I added the
onActivityCreated
, cuz without callingsetHasOptionsMenu(true);
the system will not know that this fragment needs to interact with the menu.then I removed the line
inflater.inflate(R.menu.main, menu);
because it doubled the menu items since Activity inflated a menu, then Fragment inflated another menuThanks to @David and @Rookie
使用
Fragments
时,您仍然需要使用Activity
来控制和分配Fragments
。此
Activity
可以像以前一样具有搜索功能。我最近从“普通”基于
Activity
的应用程序切换到基于Fragment
的应用程序,并且搜索功能对我来说同样有效。您是否尝试过这样做,但没有成功?如果是这样,请在您的问题中提供更多详细信息。
编辑:
如果您想要进行特定于片段的搜索,请让所有
Fragments
使用startSearch
方法扩展接口MyFragment
,并让您的 < code>Activity 的startSearch
方法调用当前片段的startSearch
方法。When working with
Fragments
you still need to use anActivity
to control and assign theFragments
.This
Activity
can have search functionality as before.I've recently switched from a 'normal'
Activity
based app, to aFragment
based app and the search functionality worked just the same for me.Have you tried working on it, and didn't succeed? If so give some more detail in your question.
EDIT:
If you want to have a fragment specific search, have all your
Fragments
extend an interfaceMyFragment
with astartSearch
method, and have yourActivity
'sstartSearch
method call the current fragment'sstartSearch
method.我想我已经实现了:您实际上可以使用片段并将搜索图标添加到操作栏,以便可以在片段内进行搜索。诀窍是使用操作栏、操作视图、监听器、加载器和适配器。
尽管它完全绕过了 android 平台的搜索机制,但它工作得很好(但它可以通过一些工作来找到 @Alex Lockwood 描述的内容并将搜索传递给片段来完成)。在活动的情况下,它不会对意图做出预期的反应,但它可以工作:用户可以在片段内搜索。
这是代码:
搜索片段菜单的xml文件res/menu/menu_search.xml:
以及xml布局文件res/layout/search_in_fragments.xml
I think I achieved it : you can actually use fragments and add a search icon to an action bar so that a search is possible inside the fragments. The trick is to use an action bar, an action view, a listener for it, a loader and an adapter of course.
This works pretty well although it completely bypasses the android platform search mechanism (but it could be completed with some work to find what @Alex Lockwood describes and pass the search to fragments). It would not react to an intent as expected in the case of an activity, but it works : users can search inside fragments.
Here is the code :
The xml file for the menu of the search fragments res/menu/menu_search.xml:
And the xml layout file res/layout/search_in_fragments.xml
使用
ActionBar
和SearchView
。您将能够在没有与 Activity 任何连接的情况下处理搜索。只需为 SearchView 设置一个OnQueryTextListener
即可。有关自定义搜索的更多详细信息,请参阅此帖子。
Use the
ActionBar
andSearchView
. You will be able to handle searches without any connection to Activity. Just set anOnQueryTextListener
to the SearchView.See this post for more details on custom search.
其他解决方案......我不喜欢那样。
这对我来说更容易。但这是我的想法。我在等待你的意见
others solution..... i dont like that.
This more easy for me.But its my idea.Im waiting yours opinion
Fragment
不能存在于Activity
之外,Fragment
也不能链接到android.intent.action.SEARCH
> 或任何其他意图过滤器
。因此,如果不使用
Activity
来包装Fragment
,您所要求的就是不可能的。A
Fragment
cannot exist outside anActivity
, nor can aFragment
be linked to aandroid.intent.action.SEARCH
or any otherintent-filter
.So without using an
Activity
to wrap the theFragment
, what you're asking is not possible.如果将片段放入 ViewPager,当我不在想要提供搜索栏的片段上时,我可以通过阻止搜索按钮来完成此操作。进入活动:
在没有物理搜索按钮的情况下,我在片段中添加了一个操作项,这会触发以下代码:
In case of Fragments into a ViewPager, I could do it by blocking the search button when I'm not on the fragment where I want to give a search bar. Into the activity:
And in case of no physical search button, I added an action item into the fragment, which trigger this code:
我找到了解决办法:)
您可以在 BaseActivity 中重写此方法(startActivity(Intent)),然后检查操作是否为 ACTION_SEARCH 然后执行您的特殊工作:D
i found a work around :)
you can override this method (startActivity(Intent) ) in your BaseActivity and then check if the action is ACTION_SEARCH then do your special job :D
是的,它可能,
请在您的 Activity 上实现搜索视图,Activity 中的“onQueryTextChange”也会侦听片段中的搜索,您可以检查“onQueryTextChange”中的片段可见性,如果可见,您可以调用片段的搜索方法,其工作在我的代码中完美
yes its possible,
please implements Search view on your Activity ,'onQueryTextChange' in Activity will also listen the search in fragment, you can check the fragment visibility in 'onQueryTextChange' , if it visible you can call your search method for fragment, its working perfect in my code