无法单击自定义操作视图

Mac*_*rse 22 android actionview android-3.0-honeycomb

我想自定义添加ActionView到我的ActionBar.

我想添加常用的刷新按钮.(ImageButton,ProgressBar在里面FrameLayout)但如果​​我使用的ActionView onOptionsItemSelected()是从未被调用过的.

这是代码:

在我的Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.messages_actionbar, menu);
mRefreshView = (RefreshView) menu.findItem(R.id.messages_refresh).getActionView();

return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)

messages_actionbarsrc:

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

    <item android:id="@+id/messages_refresh"
        android:title="title"
        android:icon="@drawable/icon"
        android:showAsAction="always"
        android:actionViewClass="com.blabla.RefreshView"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

RefreshView的代码:

public class RefreshView extends FrameLayout {

    private ImageView mButton;
    private ProgressBar mProgressBar;
    private boolean mLoading;

    public RefreshView(Context context) {
        super(context, null);
        initView(context);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initView(context);
    }

    public RefreshView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflator = LayoutInflater.from(context);
        View v = inflator.inflate(R.layout.actionbar_refresh, this);
        mProgressBar = (ProgressBar) v.findViewById(R.id.action_refresh_progress);
        mButton = (ImageView) v.findViewById(R.id.action_refresh_button);
    }

    public void setLoading(boolean loading) {
        if (loading != mLoading) {
            mProgressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
            mButton.setVisibility(loading ? View.GONE : View.VISIBLE);
            mLoading = loading;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

actionbar_refresh的src代码:

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

    <ImageView
        android:id="@+id/action_refresh_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="center"
        android:background="@drawable/icon" />

    <ProgressBar
        android:id="@+id/action_refresh_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        android:indeterminate="true" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

另一方面,如果我在类中设置了一个它被调用clickListenerImageView内部RefreshView.

有人这样做了吗?

小智 8

onOptionsItemSelected()只有在操作项位于您应该处理的溢出菜单中时才应该调用.(你必须在行动栏上"永远",所以onOptionsItemSelected()不会被召唤).

onCreateOptionsMenu()充气后,您必须OnMenuItemClickListener为菜单项设置一个.


Mac*_*rse 3

我最终使用了http://code.google.com/p/styled-action-bar/中的 src 代码。