문제

나는 정말로 이상한 문제가있다.나는 파편이있는 호출기가있다.조각 중 하나는 메시지 조각입니다.이 조각에서는 3 개의 탭 단편이있는 fragmenttabhost가 있습니다.액션 바에서는 SearchView가 있습니다.뭔가를 검색하고 싶습니다. 쿼리 문자열을 작성하기 시작하면 검색보기의 초점이 제거되고 Tabhost의 첫 번째 탭으로 설정됩니다.그런 식으로 쿼리 문자열을 쓸 수 없습니다.이것은 내가 메시지를 근처에있을 때만 발생합니다.예를 들면 : 내가 충분히 멀리있는 경우 (1 페이지), 검색이 잘 작동합니다.

누군가가 문제가 무엇인지 말해 줄 수 있습니까?

여기에 그림이 있습니다. Android tabhost 문제

메시지 필수 :

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;

My 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