与Android的碎片搜索 [英] Android search with Fragments

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

问题描述

是否有人知道一个教程或如何执行标准 Android的搜索界面与片段 S'换句话说,是有可能把一个 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?

推荐答案

总之,你不能。这里有几个原因在创建一个搜索界面片段是不可能的。

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

  1. 在创建一个可搜索的界面,您必须指定一个默认的搜索活动在你的Andr​​oid清单。正如我敢肯定你知道,一个片段没有父母就不能存在活动,因此,这种分离是不可能的

  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操作的意图。您的搜索   活动从检索意图的查询中查询额外的费用,   搜索您的数据和presents的结果。

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.

底层,内部系统,负责提供搜索结果期望一个活动,不是片段;因此,实现一个搜索界面是完全独立于活动是不可能的,因为它需要改变的底层系统本身的。退房源$ C ​​$ C的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/app/SearchableInfo.java"><$c$c>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 :).

话虽这么说,它似乎并不像它会太难以实现类似于你所描述的东西。例如,你可能会考虑实施的可搜索活动,以便将接受 android.intent.action.SEARCH 意图和(而不是立即显示的结果在的ListView ,例如)将通过搜索查询到你的片段秒。例如,考虑下面的搜索活动:

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
    }
}

当一个搜索请求时,系统会启动您的搜索活动,执行查询,并会把结果在一定集装箱活动(根据您的实施 doMySearch )。然后,容器的活动将通过这些成果所包含的搜索片段,其中会显示结果。实施需要使用比你可能希望有更多的工作,但我敢肯定有办法,你可以把它更加模块化,而且看起来这可能是最好的,你可以做。

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.

P.S。如果你使用这种方法,您可能需要特别注意哪些Activitys添加/删除到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。您可能还忘了标准的搜索界面完全和公正的实现在片段中的 下面拉哈夫的帖子

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.

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

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