不能单击“定制操作视图”

|| 我试图将自定义“ 0”添加到我的“ 1”中。 我正在尝试添加常见的刷新按钮。 (
ImageButton
FrameLayout
内的
ProgressBar
),但如果我使用
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
的src:
<?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
的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>
另一方面,如果我在
RefreshView
类内将
clickListener
设置为ѭ16it,则会被调用。 有人做过吗?     
已邀请:
仅当操作项目在溢出菜单中且还应处理时,才应调用6。 (您必须在操作栏上将其强制“始终”设置为“ 6”,否则不会被调用)。 充气后
onCreateOptionsMenu()
,必须为菜单项设置
OnMenuItemClickListener
。     
我最终使用了http://code.google.com/p/styled-action-bar/中的src代码。     
我为我找到了一个可行的解决方案,并希望与您分享。它基于(@Macarse)的第一种方法,并进行了一些重要更改。 重要:相应地调整initView方法 将onClickListener设置为ImageView(mButton) 将loading设置为true并向Activity(MyActivity)告知点击
private void initView(final Context context) {
    final LayoutInflater inflator = LayoutInflater.from(context);

    final View actionView = inflator.inflate(
            R.layout.action_refresh_progress, this);
    mProgressBar = (ProgressBar) actionView
            .findViewById(R.id.action_refresh_progress);

    mButton = (ImageView) actionView
            .findViewById(R.id.action_refresh_button);

    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View view) {
            setLoading(true);
            ((MyActivity) context).handleRefreshButtonClick();
        }
    });
}
对活动(MyActivity)中的点击做出相应的反应
public void handleRefreshButtonClick() {
    // Start refreshing...
}
我希望我的方法可以为您节省一些寻找可行解决方案的时间!     

要回复问题请先登录注册