关于Android菜单上的记录
标签(空格分隔): Android 菜单
最近在做分享上一下事情,想遵守Android Design 规范做一个分享的菜单但是遇到一些问题
- Android上ShareProvider 不支持捕获分享(也许是我没找到),于是就不能针对某个分享项做优化
- 抛弃ShareProvider使用,单纯的使用Android的菜单分享选项,这样虽然解决了不能捕获的问题,但是也是带了一个问题,就是无法用比较少的代码来完成分享。(发现弹出对话框的分享方式是最简单省事的)
最后权衡了知识和利弊之后只能做个简单的分享类,在CreateMenu 的时候addSubMenu ,
然后在OnMenuOptionSelected() handle 操作就可以了。代码如下
package com.changheng.app.ui;import android.content.Context;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import com.changheng.app.R;import cn.sharesdk.framework.Platform;import cn.sharesdk.framework.ShareSDK;import cn.sharesdk.wechat.favorite.WechatFavorite;import cn.sharesdk.wechat.friends.Wechat;import cn.sharesdk.wechat.moments.WechatMoments;/** * Created With Android Studio * User 47 * Date 2015/2/13 * Time 14:29 * 分享助手 */public class ShareMenuHelper { private Context mContext; private SubMenu mSubMenu; private static ShareItem[] SHARE_ITEM = { new ShareItem(R.drawable.logo_wechat, R.string.wechat, ShareSDK.getPlatform(Wechat.NAME)), new ShareItem(R.drawable.logo_wechatfavorite, R.string.wechatfavorite, ShareSDK.getPlatform(WechatFavorite.NAME)), new ShareItem(R.drawable.logo_wechatmoments, R.string.wechatmoments, ShareSDK.getPlatform(WechatMoments.NAME)) }; public ShareMenuHelper(Context context,SubMenu subMenu){ mContext = context; mSubMenu = subMenu; } public void init(){ int i = 0; for (ShareItem shareItem : SHARE_ITEM){ SubMenu subMenu = mSubMenu.addSubMenu( Menu.NONE,shareItem.id = i++,Menu.NONE, shareItem.titleRes); subMenu.setIcon(shareItem.iconId); } } public void handlerMenuSelected(MenuItem menuItem, Platform.ShareParams shareParams){ int id = menuItem.getItemId(); for (ShareItem shareItem : SHARE_ITEM) { if (id == shareItem.id) doShare(shareItem,shareParams); } } private void doShare(ShareItem shareItem, Platform.ShareParams shareParams) { shareItem.platform.share(shareParams); } public static class ShareItem{ public int id; public int iconId; public int titleRes; public Platform platform; public ShareItem( int iconId, int titleRes, Platform platform) { this.iconId = iconId; this.titleRes = titleRes; this.platform = platform; } }}
这个东西主要比较纠结的地方在于,如何获取submenu。找了好久才发现代码是这样的
menuItem.getSubMenu();