Android TabHost 和 SearchView 焦点问题

标签 android searchview fragment-tab-host

我有一个非常奇怪的问题。我有一个带有 fragment 的寻呼机。 fragment 之一是消息 fragment 。在此 fragment 中,我有一个包含三个选项卡 fragment 的 FragmentTabHost。在操作栏中我有一个 SearchView。当我想要搜索某些内容并开始编写查询字符串时,搜索 View 的焦点将从其中删除并设置为 TabHost 的第一个选项卡。这样我就无法编写查询字符串。仅当我靠近 MessagesFragment 时才会发生这种情况。例如:如果我足够远(1 页),则搜索工作正常。

有人可以告诉我问题出在哪里吗?

这是一张图片: Android TabHost Issue

消息 fragment :

public class MessagesFragment extends Fragment {
private FragmentTabHost tabHost;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    this.tabHost = new FragmentTabHost(this.getActivity());
    this.tabHost.setup(this.getActivity(), this.getChildFragmentManager(), container.getId());

    TabSpec tabSpecForAllMessages = this.tabHost.newTabSpec("all").setIndicator("ALL");
    this.tabHost.addTab(tabSpecForAllMessages, AllMessagesFragment.class, null);

    TabSpec tabSpecForSentMessages = this.tabHost.newTabSpec("sent").setIndicator("SENT");
    this.tabHost.addTab(tabSpecForSentMessages, SentMessagesFragment.class, null);

    TabSpec tabSpecForReceivedMessages = this.tabHost.newTabSpec("received").setIndicator("RECEIVED");
    this.tabHost.addTab(tabSpecForReceivedMessages, ReceivedMessagesFragment.class, null);

    return this.tabHost;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    this.tabHost = null;
}

}

选项卡主机布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>
</LinearLayout>

</TabHost>

操作栏菜单项:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/menu_item_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:title="@string/title_search" />

<item
    android:id="@+id/menu_item_home"
    android:icon="@drawable/ic_action_home"
    android:showAsAction="always"
    android:title="@string/title_home" />

onCreateOptionsMenu 事件处理程序:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = this.getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(this
            .getComponentName()));

    return true;
}

最佳答案

尝试添加方法

@Override
public boolean onQueryTextChange(String text) {
    if(searchView!=null)
        searchView.requestFocusFromTouch();

    return true;
}

searchView 必须是 init SearchView searchView;

我的 onCreateOptionsMenu 方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.only_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            searchView.requestFocusFromTouch();
            return true;
        }
    });
}

这对我有用!

关于Android TabHost 和 SearchView 焦点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016641/

相关文章:

java - Firebase 回收适配器,带有使用 searchview 的过滤器

java - Android:如何让 SearchView 关闭按钮返回 setQueryHint()?

java - 无法解析 com.google.android.gms play-services-auth :11. 4.0

安卓搜索 View : What is the difference between setOnFocusChangeListener() and setOnQueryTextFocusChangeListener?

android - findViewById(R.id.tabhost) 始终返回 null

android - TabHost 和 FragmentTabHost 的区别

Android,FragmentTabHost - 如何去除蓝色?

java - Gson- 将 JSON 对象的 JSON 数组解析为 ArrayList<org.json.JSONObject>

android - 如何使用 Facebook android SDK 集成 android 应用程序在 facebook 墙上发布消息

android - 将 Android 应用程序限制为仅适用于 armv7 设备