使用片段的ActionBar中的SearchView [英] SearchView in ActionBar using Fragments

查看:60
本文介绍了使用片段的ActionBar中的SearchView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HomeActivity中有一个导航抽屉.在导航中,我有每个都包含不同RecycleViews的片段.我想在ActionBar中实现SearchView,以便它将根据活动片段在RecycleView中进行搜索.

I have a navigation drawer in HomeActivity. Inside the navigation I have fragments that contain different RecycleViews each. I want to implement SearchView in ActionBar so it will search in the RecycleView according to the active fragment.

这是片段代码中的一个:

This is on of the fragments code:

   
   public class HomeFragment extends Fragment {

    private static BackEnd backEnd;
    private RecyclerView rvBooks;
    private BookAdapter adapter;
    private MenuItem mSearchAction;
    private SearchView searchView;
    private ActionBar actionBar;
    private boolean isSearchOpened = false;
    private EditText etSearch;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        rvBooks = (RecyclerView) view.findViewById(R.id.rv_books);
        rvBooks.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new BookAdapter(getActivity());
        rvBooks.setAdapter(adapter);
        try {
            backEnd = BackEndFactory.getInstance(getActivity());
            for(int i = 0; i < 10; i++)
            {
                backEnd.addBook(new Book(R.mipmap.ic_launcher, "Genere "+(i+1), "Book "+(i+1), "2015", "Author", 100));
            }
            adapter.setBookList(backEnd.getBooksList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.home, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.action_search:
                handleMenuSearch();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        mSearchAction = menu.findItem(R.id.action_search);
        searchView = (SearchView) mSearchAction.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                adapter.clearData();
                adapter.setBookList(backEnd.searchForBook(query));
                return true;
            }

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

    private void handleMenuSearch() {
        actionBar = getSupportActionBar(); //get the actionbar

        if(isSearchOpened){ //test if the search is open
            if(actionBar != null)
            {
                actionBar.setDisplayShowCustomEnabled(false);   //disable a custom view inside the actionbar
                actionBar.setDisplayShowTitleEnabled(true);     //show the title in the action bar
            }

            // hides the keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);

            // add the search icon in the action bar
            mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_search));

            isSearchOpened = false;
        } else { // open the search entry
            if(actionBar != null)
            {
                actionBar.setDisplayShowCustomEnabled(true); //enable it to display a
                // custom view in the action bar.
                // action.setCustomView(R.layout.search_bar);//add the custom view

                actionBar.setDisplayShowTitleEnabled(false); //hide the title
            }
            // open the keyboard focused in the edtSearch
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
            // add the close icon
            mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_search));

            isSearchOpened = true;
        }
    }
}

我在 handleMenuSearch()函数中遇到问题:由于片段不是活动,因此无法识别 getSupportActionBar() getSystemService(Context.INPUT_METHOD_SERVICE).

I have problems in handleMenuSearch() function: getSupportActionBar() and getSystemService(Context.INPUT_METHOD_SERVICE) are not recognized because fragments are not activities.

那么,请问如何解决?

推荐答案

您需要通过调用 getActivity()来获取实际的父上下文(即父Activity),如下所示:

You need to get the actual parent context (which is the parent Activity) by calling getActivity() as follows:

getActivity().getSupportActionBar();
...
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
...

这篇关于使用片段的ActionBar中的SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆