Android TabhostとSearchViewフォーカスの問題
-
21-12-2019 - |
質問
私は本当に奇妙な問題を抱えています。私は断片を持つポケットベルを持っています。フラグメントの1つはメッセージの断片です。この断片では、3つのタブフラグメントを持つ断片TABHOSTがあります。アクションバーでは検索ビューがあります。何かを検索してクエリ文字列の書き込みを開始したい場合は、検索ビューの焦点が削除され、TabHostの最初のタブに設定されます。そのように、クエリ文字列を書き込めません。これは私がメッセージの範囲に近いときにのみ起こります。例えば:私が十分なのなら(1ページ)の場合、検索はうまく機能しています。
誰かが問題が何であるかを教えてくれる?
これは画像です:
メッセージフラグメント:
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でなければなりません。
マイングクレーションオプションメン法:
@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;
}
});
. 所属していません StackOverflow