简体   繁体   中英

Home up button and SearchView issue in Action Bar in version 2.3

I am stuck on a part in my app. My app's minimum sdk version is v2.3. I am working on action bar. When I click on search icon on the action bar, it expands, but not fully. Two icons on the right side are still visible. And when I click on the home up button,it closes the SearchView, but doesn't call onOptionsItemSelected method.

Note : Home up button calls the onOptionsItemSelected method when clicked at first(when searchview is not expanded), but does not call onOptionsItemSelected when searchview is expanded.

I am using support library as I am working on very old version. Please help me.

My code is as follows:

menu_main.xml

<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/search_action"
    app:showAsAction="always|collapseActionView"
    android:title="@string/search_action"
    android:icon="@drawable/search_icon"
    app:actionViewClass="android.support.v7.widget.SearchView"
    />

<item
    android:id="@+id/shopping_cart"
    app:actionLayout="@layout/badge_layout"
    android:title="@string/shopping_cart"
    app:showAsAction="always"
    >
</item>

<item
    android:id="@+id/user_menu"
    android:icon="@drawable/overflow_icon"
    app:showAsAction="always"
    android:title="user">
</item>
</menu>

Styles.xml

<resources  xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <!--<item name="android:background"  tools:ignore="NewApi">@color/action_bar_color</item>-->
    <item name="background">@color/action_bar_color</item>
    <item name="logo">@drawable/nt_logo</item>
    <item name="displayOptions">useLogo|showHome</item>
</style>

MainActivity.java

package com.example.stickyheader.stickyheader;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem searchItem = menu.findItem(R.id.search_action);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint("Search here");
    searchView.setIconifiedByDefault(true);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();



    return super.onOptionsItemSelected(item);
}
}

I have sorted this issue in another way. I created a custom SearchView and attached two listeners ie expandListener and collapseListener. On expand, I made all the menu items invisible and vice versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM