我有一个非常奇怪的问题。我有一个带碎片的寻呼机。其中一个片段是消息片段。在这个片段中,我有一个带有三个标签片段的fragmenttabhost。在动作栏中,我有一个搜索范围。当我想搜索某些东西并且我开始编写查询字符串时,从中删除搜索视图的焦点,并设置为TabHost的第一个选项卡。这样我无法编写查询字符串。只有在靠近消息附近时才发生这种情况。例如:如果我足够了(1页),搜索正常工作。

有人可以告诉我问题是什么?

这是一张图片:

messagesfragment:

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 init shigw view搜索展览范围;

我的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;
        }
    });
}
.

它为我工作了!

其他提示

我也面临着同样的问题,我通过将侦听器写入这样的观点来解决这个问题。

searchView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            searchView.requestFocusFromTouch();
            return true;
        }
    });
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top