2017-04-24 110 views
1

我試圖在片段中實現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> 
+0

我們可以看到用於'svZelal'(SearchView)的菜單文件。 – pRaNaY

+1

我認爲你需要在fragment中聲明它的賦值.setHasMenuOptions(true)並實現onCreateOptions和點擊處理方法。 –

回答

0

您需要使用searchView作爲菜單項,則需要使用以下代碼在您的片段中添加菜單:

private MenuItem mSearchItem; 
private SearchView sv; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

setHasOptionsMenu(true); 
}  


@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu_search, menu); // Put your search menu in "menu_search" menu file. 
    mSearchItem = menu.findItem(R.id.svZelal); 
    sv = (SearchView) MenuItemCompat.getActionView(mSearchItem); 
    sv.setIconified(true); 

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE); 
    sv.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); 
    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      mSearchView.clearFocus(); 
      return true; 
     } 

     @Override 
     public boolean onQueryTextChange(String query) { 
      adapter.getFilter().filter(query); 
      return true; 
     } 
    }); 
super.onCreateOptionsMenu(menu, inflater); 
} 

我希望它能幫助你。

+0

這個例子中的mSearchView是什麼? –

+0

這是代碼錯字。我剛剛更新了我的答案。請檢查 – pRaNaY

+0

是的,我知道得到它的工作謝謝 –

0

我認爲你必須綁定搜索查看在MainActivity並宣佈public static SearchView searchView;,然後在片段使用像MainActivity.searchView.setQueryHint("hint");

+0

搜索視圖在操作欄 –