24

我正在尝试向ActionView我的ActionBar.

我正在尝试添加通用刷新按钮。( ImageButton,ProgressBar在 a FrameLayout) 但如果我使用 anActionView 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>

另一方面,如果我在类内部设置 a clickListener,它就会被调用。ImageViewRefreshView

有人已经这样做了吗?

4

3 回答 3

8

onOptionsItemSelected()仅当操作项位于您还应处理的溢出菜单中时才应调用。(你让它在操作栏上“总是”,所以onOptionsItemSelected()不会被调用)。

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

于 2011-04-17T12:59:16.897 回答
3

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

于 2011-04-17T13:27:08.097 回答
1

我为我找到了一个可行的解决方案,我想与您分享。它基于 (@Macarse) 的第一种方法,并进行了一些重要更改。

重要提示:相应地调整initView方法

  1. onClickListener设置为 ImageView ( mButton )

  2. 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();
            }
        });
    }
    
  3. 对 Activity ( MyActivity ) 中的点击做出相应反应

    public void handleRefreshButtonClick() {
        // Start refreshing...
    }
    

我希望我的方法可以为您节省一些时间来寻找可行的解决方案!

于 2013-03-16T20:28:07.243 回答