设置展开侦听器后菜单 ActionView 停止展开

Menu ActionView stops expanding after setting expand listener

我想在工具栏菜单中设置自定义搜索布局。我的问题是当我为搜索项设置扩展侦听器时 - 操作视图不再扩展。
我试着打电话给

item.expandActionView();
MenuItemCompat.expandActionView(item);

在 onOptionsItemSelected 中,但它不起作用。
我的代码:

menu_projects.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:actionLayout="@layout/search_layout"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:showAsAction="always|collapseActionView"/>
</menu>


search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/closeSearchImageView"
        android:layout_toStartOf="@+id/closeSearchImageView"
        android:background="@null"
        android:hint="@string/search"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textCapSentences"
        android:paddingLeft="10dp"
        android:paddingRight="5dp"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textColorHint="#7dffffff"
        android:textCursorDrawable="@drawable/cursor"/>

    <ImageView
        android:id="@+id/closeSearchImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="3dp"
        android:src="@drawable/abc_ic_clear_mtrl_alpha"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_below="@+id/searchEditText"
        android:background="#7dffffff"/>
</RelativeLayout>


内部片段

@Override
public void onPrepareOptionsMenu(Menu menu) {
            searchViewExpandCollapse = new SearchViewExpandCollapse(menu);//fragment field
            super.onPrepareOptionsMenu(menu);
}

private class SearchViewExpandCollapse implements MenuItemCompat.OnActionExpandListener
{
    private Menu menu;

    public SearchViewExpandCollapse(Menu menu) {
        this.menu = menu;
        MenuItem searchItem = menu.findItem(R.id.action_search);
        MenuItemCompat.setOnActionExpandListener(searchItem, this);//action view not expanding with this 
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return false;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        return false;
    }
}


有什么想法吗?

我找到了解决方案。首先,我在设置侦听器后以编程方式将 ActionView 设置为 MenuItem。之后,我将侦听器方法的 return 值从 false 更改为 true。也许会对某人有所帮助。
完整代码:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    searchController = new SearchViewExpandCollapse(menu);
    if (!query.isEmpty()) {
        searchController.expandActionView();
        searchController.setQuery(query);
    }
    super.onPrepareOptionsMenu(menu);
}

private class SearchViewExpandCollapse implements MenuItemCompat.OnActionExpandListener, OnClickListener
{
    private final EditText editText;
    private final MenuItem searchItem;
    private Menu menu;
    private View searchView;

    public SearchViewExpandCollapse(Menu menu) {
        this.menu = menu;
        searchItem = menu.findItem(R.id.action_search);
        MenuItemCompat.setOnActionExpandListener(searchItem, this);
        searchItem.setActionView(R.layout.search_layout);
        searchView = searchItem.getActionView();
        editText = (EditText) searchView.findViewById(R.id.searchEditText);
        editText.addTextChangedListener(textWatcher);
        searchView.findViewById(R.id.closeSearchImageView).setOnClickListener(this);
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        menu.setGroupVisible(R.id.main_group, false);
        editText.requestFocus();
        editText.post(new Runnable()
        {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            }
        });
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        menu.setGroupVisible(R.id.main_group, true);
        editText.clearFocus();
        InputMethodManager inputManager = (InputMethodManager) getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        return true;
    }

    private TextWatcher textWatcher = new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            query = s.toString();
            searchAdapter.getFilter().filter(query);
        }
    };

    public void expandActionView() {
        searchItem.expandActionView();
    }

    public void setQuery(String query) {
        editText.removeTextChangedListener(textWatcher);
        editText.setText(query);
        editText.setSelection(query.length());
        editText.addTextChangedListener(textWatcher);
    }

    public void onClick(View v) {
        if (editText.getText().length() != 0) {
            editText.setText("");
        } else {
            searchItem.collapseActionView();
        }
    }
}

如果其他人后来遇到同样的问题(比如我),问题就在这里:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    return false;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    return false;
}

通过返回 false,您可以覆盖菜单项的原始操作。这应该是:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    //what you want to do
    return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    //what you want to do
    return true;
}