使用 Fragment 进行 Android 搜索 [英] Android search with Fragments

查看:37
本文介绍了使用 Fragment 进行 Android 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何实现标准 Android 搜索界面 与 Fragment 的?换句话说,是否可以在 Fragment 中使用 SearchManager 进行标准搜索?

Does somebody know of a tutorial or an example of how to implement the standard Android search interface with Fragments? In other words, is it possible to put a standard search with a SearchManager in a Fragment?

推荐答案

简而言之,你不能.无法在 Fragment 中创建搜索界面的原因有几个.

In short, you can't. There are a couple of reasons why creating a search interface within a Fragment is not possible.

  1. 创建可搜索界面时,您必须在 Android 清单中指定默认的可搜索活动".我相信你知道,没有父 Activity 就不可能存在 Fragment,因此,这种分离是不可能的.

  1. 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 parent Activity and thus, this separation is not possible.

如果你已经找到了#1,我假设你问这个问题是希望有一些神奇的黑客"可以完成工作.但是,文档指出,

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,

当用户在搜索对话框或小部件中执行搜索时,系统启动您的可搜索活动并提供搜索使用 ACTION_SEARCH 操作在 Intent 中查询.您的可搜索活动从意图的 QUERY extra 中检索查询,然后搜索您的数据并显示结果.

When the user executes a search in the search dialog or widget, the system starts your searchable activity and delivers it the search query in an Intent with the ACTION_SEARCH action. Your searchable activity retrieves the query from the intent's QUERY extra, then searches your data and presents the results.

负责提供搜索结果的底层内部系统需要一个Activity,而不是一个Fragment;因此,实现完全独立于 Activity 的搜索界面是不可能的,因为它需要对底层系统本身进行更改.查看 SearchableInfo 类,如果你不相信我 :)

The underlying, internal system that is responsible for providing search results expects an Activity, not a Fragment; thus, implementing a search interface that is completely independent of an Activity is not possible, as it would require changes to the underlying system itself. Check out the source code for the SearchableInfo class if you don't believe me :).

话虽如此,实现与您所描述的类似的东西似乎并不难.例如,您可能会考虑实现您的 searchable-Activity 以便它接受 android.intent.action.SEARCH 意图和(而不是立即在 ListView 中显示结果),例如)将搜索查询传递给您的 Fragments.例如,考虑以下可搜索活动:

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 a ListView, for example) will pass the search query to your Fragments. For instance, consider the following searchable Activity:

public class SearchableActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          doMySearch(query);
        }
    }

    /**
     * Performs a search and passes the results to the container
     * Activity that holds your Fragments.
     */
    public void doMySearch(String query) {
        // TODO: implement this
    }
}

当发出搜索请求时,系统将启动您的可搜索 Activity,执行查询,并将结果传递给某个容器 Activity(基于您对 doMySearch 的实现).然后容器 Activity 会将这些结果传递给包含的可搜索 Fragment,其中将显示结果.实现需要的工作比您可能希望的要多一些,但我相信您可以通过多种方法使其更加模块化,而且这似乎是您能做的最好的事情.

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 searchable Fragment, 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.

附言如果您使用这种方法,您可能需要特别注意哪些 Activity 被添加/删除到 backstack.请参阅此帖子,了解有关如何完成此操作的更多信息.

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.您可能还完全忘记了标准搜索界面,而只是在 Fragment 中实现一个简单的搜索,如Raghav 的帖子如下.

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.

这篇关于使用 Fragment 进行 Android 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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