我試圖在片段中實現searchView函數。 searchView是在一個自定義操作欄這是我使用的代碼,但它不工作。如果我將searchview移動到片段佈局上,但它在操作欄中不起作用。SearchView不工作在片段
public class TabActivity_adbya extends Fragment implements View.OnTouchListener,AdapterView.OnItemClickListener {
GridView gridView;
FirebaseDatabase firebaseDatabaseInstance;
private DatabaseReference booksInstance;
SearchView sv;
AlbumDBHandler db;
ArrayList<mokdmatData> books = new ArrayList<>();
maKotabAdapter adapter;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstancestate) {
final View rootView = inflater.inflate(R.layout.activity_tab_adbya,container,false);
((makalat2) getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
((makalat2) getActivity()).getSupportActionBar().setCustomView(R.layout.abs_layout);
((makalat2) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);
TextView textViewTitle = (TextView) getActivity().findViewById(R.id.mytext);
textViewTitle.setText("Articles");
sv = (SearchView) getActivity().findViewById(R.id.svZelal);
sv.setQueryHint("Search...");
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return true;
}
});
gridView = (GridView) rootView.findViewById(R.id.gridView_adbya);
gridView.setOnItemClickListener(this);
db = new AlbumDBHandler(getActivity());
getData();
init();
return rootView;
}
這是自定義操作欄我使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>
<ImageView
android:layout_width="0dp"
android:layout_height="22dp"
android:src="@drawable/homebutton"
android:layout_gravity="center"
android:layout_weight="0.40"
android:id="@+id/homeButton"
/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.60"
/>
<com.mediamer.metwally.saidkotb.helpers.CustomTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="@color/TextColor"
android:id="@+id/mytext"
android:textSize="25sp"
android:layout_weight="1"
/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.60"
/>
<android.support.v7.widget.SearchView
android:id="@+id/svZelal"
android:layout_width="0dp"
android:searchIcon="@android:drawable/ic_menu_search"
android:layout_height="match_parent"
android:layout_weight="0.40"
/>
</LinearLayout>
</LinearLayout>
我們可以看到用於'svZelal'(SearchView)的菜單文件。 – pRaNaY
我認爲你需要在fragment中聲明它的賦值.setHasMenuOptions(true)並實現onCreateOptions和點擊處理方法。 –