无法关闭actionBar中的搜索框

时间:2014-02-15 02:14:56

标签: android android-fragments android-actionbar

我的android actionBar中有一个搜索图标。在我的搜索片段中,它会自动展开并打开键盘进行搜索。

搜索时会填充列表视图,然后单击项目会加载新片段。我试图在新片段加载时将搜索框关闭回图标,这样键盘就会消失,但它不起作用。

我打开并应关闭搜索框的片段的代码是:

public class BeerPage extends Fragment {

    public BeerPage(){}

    String beerId = "";
    SearchView searchView;

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

        View rootView = inflater.inflate(R.layout.beer_page, container, false);
        setHasOptionsMenu(true);
        getActivity().setTitle("Beer");

        //body code goes here

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

        //get bundled data
        Bundle args = getArguments();
        beerId = args.getString("beerIDSent");

        TextView bt = (TextView)rootView.findViewById(R.id.beerBreweryName);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do whatever stuff you wanna do here
                //todo: launch new brewery page
                Fragment Fragment_one;
                FragmentManager man= getFragmentManager();
                FragmentTransaction tran = man.beginTransaction();
                Fragment_one = new BreweryPage();
                final Bundle bundle = new Bundle();
                bundle.putString("beerIDSent", beerId);
                Fragment_one.setArguments(bundle);
                tran.replace(R.id.main, Fragment_one);//tran.
                tran.addToBackStack(null);
                tran.commit();
            }
        });
        TextView bt2 = (TextView)rootView.findViewById(R.id.beerStyleName);
        bt2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do whatever stuff you wanna do here
                //todo: launch new brewery page
                Fragment Fragment_two;
                FragmentManager man= getFragmentManager();
                FragmentTransaction tran = man.beginTransaction();
                Fragment_two = new StylePage2();
                final Bundle bundle = new Bundle();
                bundle.putString("beerIDSent", beerId);
                Fragment_two.setArguments(bundle);
                tran.replace(R.id.main, Fragment_two);//tran.
                tran.addToBackStack(null);
                tran.commit();  
            }
        });

        Button bt3 = (Button)rootView.findViewById(R.id.buttonTasteTag);
        bt3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do whatever stuff you wanna do here
                //todo: launch new brewery page
                Fragment Fragment_three;
                FragmentManager man= getFragmentManager();
                FragmentTransaction tran = man.beginTransaction();
                Fragment_three = new TasteTags2();
                final Bundle bundle = new Bundle();
                bundle.putString("beerIDSent", beerId);
                Fragment_three.setArguments(bundle);
                tran.replace(R.id.main, Fragment_three);//tran.
                tran.addToBackStack(null);
                tran.commit();
            }
        });

        String url = "http://api.brewerydb.com/v2/beer/"+ beerId +"?key=0cdb22f84315834cc807c0176a927060&format=json&withBreweries=y";

        new GetBeerDataJSON(getActivity(), beerId).execute(url);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu, inflater);
        //inflater.inflate(R.menu.main, menu);
        searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView();
        searchView.setIconified(true);
    } 
}

1 个答案:

答案 0 :(得分:0)

首先,耶!啤酒!

其次,你试过了吗?

searchView.collapseActionView();

我正在努力解决SearchView的问题,并在onTabSelected()中调用了collapseActionView(),这对我有用。所以如果你使用标签,你可以试试。

干杯!

http://developer.android.com/reference/android/view/MenuItem.html#collapseActionView()