android - 无法单击自定义操作 View

标签 android android-3.0-honeycomb actionview

我正在尝试将自定义 ActionView 添加到我的 ActionBar

我正在尝试添加通用刷新按钮。 (ImageButton, ProgressBarFrameLayout 内)但如果我使用 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);
}

messages_actionbar的源代码:

<?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>

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;
        }
    }
}

actionbar_refresh的源代码:

<?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>

另一方面,如果我在 RefreshView 类中将 clickListener 设置为 ImageView,它会被调用。

有人已经这样做了吗?

最佳答案

onOptionsItemSelected() 仅当操作项位于您也应处理的溢出菜单中时才应调用。 (您必须将其强制“始终”显示在操作栏上,因此不会调用 onOptionsItemSelected())。

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

关于android - 无法单击自定义操作 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446126/

相关文章:

android - 如何从 fragment 导航组件中的后台堆栈中只删除一个 fragment ?

Android - 从 db 创建一个可绘制对象数组

ruby-on-rails - 已将Rails 4升级到Rail 5,现在为#<ActionView::Template::Error:0x007f243ecd5d48>获得 "NoMethodError: undefined method ` original_exception'”

android - 如何动态显示 Android fragment

android - TextView setScaleX()/setScaleY() 和 setTextIsSelectable(true) 选择

安卓 : URI for open an image with ACTION View

android - 使用单个请求将多个文件上传到 Parse 服务器

android - 具有Firebase InstanceId接收器的ANR

android - 在单个 Activity 中使用多个 fragment