2012-12-06 4 views
2

ActionbarSherlock ver 4.2에서 "검색"을 시도하고 있습니다. ActionbarSherlock은 SerchView를 최신 버전으로 백 포트했습니다. 나는 "새로 고침"는 ABS의 버튼 만 클릭하면ActionbarSherlock을 포함한 작업 표시 줄에서 "SearchView"사용

나는 나는 토스트를

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case Menu.FIRST: 
      Toast.makeText(getActivity(),"FIRST", Toast.LENGTH_SHORT).show(); 
      break; 
     case Menu.FIRST + 1: 
      Toast.makeText(getActivity(),"FIRST+1", Toast.LENGTH_SHORT).show(); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

에서 SherlockListFragment

@Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     // Place an action bar item for searching. 
     SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext()); 
     searchView.setQueryHint("Search Friends"); 
     searchView.setIconified(true); 

     menu.add(Menu.NONE, Menu.FIRST, Menu.FIRST, "Refresh") 
       .setIcon(R.drawable.ic_action_refresh) 
       .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 
     menu.add(Menu.NONE, Menu.FIRST + 1, Menu.FIRST + 1, "Search") 
       .setIcon(R.drawable.abs__ic_search) 
       .setActionView(searchView) 
       .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

    } 

onCreateOptionsMenu에 다음 코드를 다음 코드를 얻고있다 "검색"을 클릭하면 확장되고 EditText로 변환되지만 토스트는 시작되지 않습니다.

내 질문

어떻게 통합 할 수있는 ABS와 실행 바에서 "검색"?

+0

SDK 'SearchView'에 대한 가이드를 보았습니까? – Luksprog

+0

예, 저는 더 많은 것을 이해하게되었습니다. 나는 며칠 동안 답을 직접 게시 할 것이다. 도움이 될만한 자료가 있다면 도움이 될 것입니다. –

답변

11

작동합니다. SearchView의 구현을 위해

우리는 우리가 CursorLoader에 사용자가 입력 한 텍스트를 통과하고 적절한 결과 커서를 다시로드 할 필요 특히이 경우이

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 

      /** 
      * Called when the user submits the query. This could be due to a key press on the 
      * keyboard or due to pressing a submit button. 
      * The listener can override the standard behavior by returning true 
      * to indicate that it has handled the submit request. Otherwise return false to 
      * let the SearchView handle the submission by launching any associated intent. 
      * 
      * @param newText the query text that is to be submitted 
      * @return true if the query has been handled by the listener, false to let the 
      *   SearchView perform the default action. 
      */ 
      @Override 
      public boolean onQueryTextSubmit(String newText) { 

       return true; 
      } 


      /** 
      * Called when the query text is changed by the user. 
      * 
      * @param newText the new content of the query text field. 
      * @return false if the SearchView should perform the default action of showing any 
      *   suggestions if available, true if the action was handled by the listener. 
      */ 
      @Override 
      public boolean onQueryTextChange(String newText) { 

       ThizLog.d(TAG, "Inside onQueryTextChange"); 
       // called when the action bar search text has changed. Update 
       // the search filter, and restart the loader to do a new query 
       // with this filter. 
       String newFilter = !TextUtils.isEmpty(newText) ? newText : null; 
       // Don't do anything if the filter hasn't actually changed. 
       // Prevents restarting the loader when restoring state. 
       if (mCurFilter == null && newFilter == null) { 
        return true; 
       } 
       if (mCurFilter != null && mCurFilter.equals(newFilter)) { 
        return true; 
       } 
       mCurFilter = newFilter; 
       getLoaderManager().restartLoader(0, null, this); 
       return true; 
      } 
     }); 

같은 콜백 인터페이스를 구현해야 .

2

이 문제는 ActionBarSherlock의 version 4.3.0에서 수정되었습니다.

관련 문제