2014-04-14 22 views
15

Tôi có thanh tác vụ với biểu tượng tìm kiếm. Khi biểu tượng tìm kiếm được nhấp, nó sẽ mở rộng đến thanh tìm kiếm mà người dùng có thể nhập vào tìm kiếm.tìm kiếm trên thanh tác vụ sẽ không đóng sau khi tìm kiếm

Vấn đề là khi người dùng nhập tìm kiếm tôi muốn thanh tìm kiếm sụp đổ trở lại biểu tượng nhưng tôi không thể làm cho nó xảy ra trong cuộc đời của tôi.

đơn actionbar của tôi trông như thế này:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" > 

    <item android:id="@+id/menu_search2" 
     android:actionViewClass="android.widget.SearchView" 
     android:title="Search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:showAsAction="always|collapseActionView|" 
     android:onClick="goToSearch" 
     /> 

    <item android:id="@+id/action_scan" 
     android:icon="@drawable/barcode" 
     android:onClick="scanBarcode" 
     android:showAsAction="ifRoom|collapseActionView" 
     /> 

</menu> 

hoạt động tìm kiếm của tôi trông như thế này:

public class Search extends Fragment implements SearchView.OnQueryTextListener, ReadJSONResult.OnArticleSelectedListener { 

    private ListView lv; 
    View v; 
    SearchView searchView; 
    private SearchView mSearchView; 
    private MenuItem mSearchMenuItem; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     //set layout here 
     v = inflater.inflate(R.layout.activity_search, container, false); 
     setHasOptionsMenu(true); 
     getActivity().setTitle("Search"); 


     //get user information 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     String userName = prefs.getString("userName", null); 
     String userID = prefs.getString("userID", null); 


     return v; 


} 

    @Override 
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { 
     // Inflate the menu; this adds items to the action bar if it is present. 

     searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView(); 
     searchView.setOnQueryTextListener(this); 
     searchView.setIconified(false); 
    } 


    public boolean onQueryTextSubmit (String query) { 

     //toast query 
     //make json variables to fill 
     searchView.setIconified(true); 
     searchView.clearFocus(); 

     // url to make request 
     String url = "myURL"; 



     try { 
      query = URLEncoder.encode(query, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     String jsonUrl = url + query; 


     //todo: get json 
     ReadJSONResult task = new ReadJSONResult(getActivity()); 
     task.setOnArticleSelectedListener(this); 
     task.execute(jsonUrl); 

     return false; 
    } 





    @Override 
    public boolean onQueryTextChange(String newText) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onArticleSelected(String b, String brewery){ 
     searchView.setIconified(true); 
     searchView.clearFocus(); 
     searchView.postInvalidate(); 
     //code to execute on click 
     Fragment Fragment_one; 
     FragmentManager man= getFragmentManager(); 
     FragmentTransaction tran = man.beginTransaction(); 

     //adds beer data to shared prefs for beer tabs 
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString("beerID",b); 
     editor.putString("breweryID",brewery); 
     editor.commit(); 
     Fragment_one = new BeerTabs(); 

     tran.replace(R.id.main, Fragment_one);//tran. 
     tran.addToBackStack(null); 
     tran.commit(); 

    } 



} 

Trả lời

13

Sau khi một số tìm kiếm, tôi tìm ra giải pháp. onActionViewCollapsed hoạt động nhưng bạn có một hành vi không mong muốn (biểu tượng nhảy sang trái, chỉ báo lên ở đây ...) - giải pháp (cứng) này được đề xuất trong câu trả lời đầu tiên và trước đây của tôi và tôi đã thuyết phục bằng cách sử dụng phương thức collapseActionView. Tuy nhiên, SearchView.collapseActionView() đã không làm việc vì theo Documentation:

Collapse quan điểm hành động liên quan đến mục trình đơn này.

Nó liên quan đến MenuItem chứ không phải tiện ích SearchView. Đó là lý do tại sao bạn phải lỗi này khi bạn sử dụng phương pháp này:

Phương pháp collapseActionView() là undefined cho loại SearchView

Sau đó, giải pháp là để tạo ra một biến Menu như sau:

// At the top of your class 
private Menu mMenu; 

// onCreateOptionsMenu method 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
     this.mMenu = menu; // init the variable 
     // other stuff.. 
     return true; 
} 

// call the collapseActionView method 
public boolean onQueryTextSubmit(String query) { 
     searchView.setIconified(true); 
     searchView.clearFocus(); 
     // call your request, do some stuff.. 

     // collapse the action view 
     if (mMenu != null) { 
      (mMenu.findItem(R.id.menu_search2)).collapseActionView(); 
     } 
     return false; 
} 

Hoặc cách khác có thể tránh thực hiện SearchView.OnQueryTextListener và để thực hiện trong onCreateOptionsMenu như sau:

@Override 
// make your Menu as 'final' variable 
public void onCreateOptionsMenu (final Menu menu, MenuInflater inflater) { 
    searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView(); 
    // call the query listener directly on the SearchView 
    searchView.setOnQueryTextListener(new OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      searchView.setIconified(true); 
      searchView.clearFocus(); 

      // call the request here 

      // call collapse action view on 'MenuItem' 
      (menu.findItem(R.id.menu_search2)).collapseActionView(); 

      return false; 
     } 
     @Override 
     public boolean onQueryTextChange(String newText) { return false; } 
    }); 
    searchView.setIconified(false); 
} 

Điều này sẽ giải quyết vấn đề này. Chúc mừng mã hóa!

+0

Vì vậy mà hầu như làm việc một cách hoàn hảo, nó sụp đổ như nó nên nhưng didnt thực hiện tìm kiếm của tôi ... Mã của tôi trông như thế này bây giờ: https://gist.github.com/anonymous/cf26b36c5a22b64459b6 – Mike

+0

Đó là bởi vì bạn có trình lắng nghe truy vấn hai lần. Hãy chắc chắn rằng bạn đã xóa triển khai OnQueryListener và sao chép/dán tất cả nội dung của bạn (yêu cầu json, v.v.) ** bên trong cùng một trình lắng nghe truy vấn ** nơi bạn đang thu gọn chế độ xem. Bên trong mã ở trên cung cấp cho bạn một người nghe truy vấn. Bạn có thấy @Mike không? – Fllo

+0

Xem câu trả lời cập nhật của tôi ở trên. Gọi yêu cầu của bạn bên trong người nghe ở dòng nhận xét * "gọi yêu cầu của bạn ở đây" *.HTH @Mike – Fllo

30

Một đơn giản hơn bằng cách nào, mà làm việc ngay cả khi widget SearchView không nằm trên actionbar là đoạn mã sau:

searchView.setQuery("", false); 
searchView.setIconified(true); 

Và điều đó làm sự sụp đổ SearchView sau khi tìm kiếm được thực hiện.

+4

Điều này hoạt động hoàn hảo! –

Các vấn đề liên quan