ActionBar + ActionProvider + PopupMenu

1.概述
ActionProvider类似于Action view,它由一个自定义的layout,它用ActionProvider提供action的行为,并且可以显示子菜单
2. 实现方式有两种
1. 使用系统的ActionProvider
2. 自定义ActionProvider
3. 示例
主类:src/com/wind/actionbar/HelloActionBarActivity.java
package com.wind.actionbar;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

public class HelloActionBarActivity extends Activity{
    private static final String TAG = "HelloActionBarActivity";
	private ActionBar actionBar;
	private ShareActionProvider mShareActionProvider;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");
        setContentView(R.layout.main);
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.menu, menu);  
		// Set up ShareActionProvider's default share intent
	    MenuItem shareItem = menu.findItem(R.id.action_share);
	    mShareActionProvider = (ShareActionProvider)shareItem.getActionProvider();	//获得xml里定义的Provider
	    mShareActionProvider.setShareIntent(getDefaultIntent());	//设置点击后显示的项
		return super.onCreateOptionsMenu(menu);
	}
	
	/** Defines a default (dummy) share intent to initialize the action provider.
	  * However, as soon as the actual content to be used in the intent
	  * is known or changes, you must update the share intent by again calling
	  * mShareActionProvider.setShareIntent()
	  */
	private Intent getDefaultIntent() {
	    Intent intent = new Intent(Intent.ACTION_SEND);
	    intent.setType("image/*");
	    return intent;
	}
}

res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_about"
        android:title="@string/menu_share"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom"
        />
    
 	<item
        android:id="@+id/menu_myprovider"
        android:icon="@drawable/ic_backup"
        android:title="MyActionProvider"
        android:actionProviderClass="com.wind.actionbar.MyActionProvider"
        android:showAsAction="ifRoom"/>
</menu>

src/com/wind/actionbar/MyActionProvider.java

package com.wind.actionbar;

import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class MyActionProvider extends ActionProvider{
	private ContextWrapper mContextWrapper;
	private OnMenuItemClickListener mOnMenuItemClickListener;
	
	public MyActionProvider(Context context) {
		super(context);
		mContextWrapper = (ContextWrapper)context;
	}

	@Override
	public View onCreateActionView() {
		LayoutInflater layoutInflater = LayoutInflater.from(mContextWrapper);
	    View view = layoutInflater.inflate(R.layout.my_action_provider, null);
	    ImageView popupView = (ImageView)view.findViewById(R.id.popup_view);
        popupView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup(v);
            }
        });
        mOnMenuItemClickListener = new OnMenuItemClickListener() {
			private String TAG = "mOnMenuItemClickListener";

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				switch (item.getItemId()) {
				case R.id.Settings1:
					Log.v(TAG , "Settings1");
					break;
				case R.id.Settings2:
					Log.v(TAG , "Settings2");
					break;

				default:
					break;
				}
				return false;
			}
		};
        return view;
	}

	protected void showPopup(View v) {
		PopupMenu popup = new PopupMenu(mContextWrapper, v);
	    MenuInflater inflater = popup.getMenuInflater();
	    inflater.inflate(R.menu.popmenu, popup.getMenu());
	    popup.setOnMenuItemClickListener(mOnMenuItemClickListener);
	    popup.show();
	}

}

res/menu/popmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/Settings1" android:title="Settings1" ></item>
    <item android:id="@+id/Settings2" android:title="Settings2"></item>
</menu>

4. 部分代码的说明
<item
        android:id="@+id/menu_myprovider"
        android:icon="@drawable/ic_backup"
        android:title="MyActionProvider"
        android:actionProviderClass="com.wind.actionbar.MyActionProvider"
MyActionProvider会提供显示的View, android:icon="@drawable/ic_backup"也会指定显示的Menu,如果都存在,优先显示      MyActionProvider提供的View

你可能感兴趣的:(ActionBar + ActionProvider + PopupMenu)