24

にカスタムを追加しようとしActionViewていActionBarます。

一般的な更新ボタンを追加しようとしています。( ImageButtonProgressBara 内FrameLayout) しかし、 an を使用する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>

一方、クラス内に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. 読み込みをtrueに設定し、クリックについてアクティビティ( 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. アクティビティ(MyActivity)のクリックに応じて反応します

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

私のアプローチが、実用的な解決策を見つける時間を節約できることを願っています!

于 2013-03-16T20:28:07.243 に答える