如何在用户与ShareActionProvider选择菜单项检测? [英] How to detect when user selects menu item with ShareActionProvider?

查看:258
本文介绍了如何在用户与ShareActionProvider选择菜单项检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有小问题,我的Andr​​oid应用程序。我创建标准菜单(使用onCreateOptionsMenu)。我的一个菜单元素是分享按钮。这股元素使用OnShareTargetSelectedListener拦截其中的股票期权被点击。

I have small problem with my Android app. I'm creating standard menu (using onCreateOptionsMenu). One of my menu elements is share button. This share element is using OnShareTargetSelectedListener to intercept which of the share options is clicked.

问题是 - 我需要知道,如果用户点击共享菜单项(而不是像脸谱中的所有共享选项共享子元素,蓝牙,电子邮件等)。在onMenuItemSelected没有在共享点击呼叫。

The problem is - I need to know if user clicks this "share" menu item (not share sub-element with all the sharing options like "Facebook", "Bluetooth", "Email" etc.). In onMenuItemSelected there is no call when "share" is clicked.

有没有拦截这种共享菜单元素点击事件的任何优雅的方式?

Is there any elegant way to intercept this "share" menu-element click event?

推荐答案

好吧,这里是解决方案:增加新的侦听器接口(ShareMenuListener.java):

Ok, here is the solution: add new listener interface (ShareMenuListener.java):

public interface ShareMenuListener {
    public void onMenuVisibilityChanged(boolean visible);

}

扩展ShareActionProvider(CustomShareActionProvider.java)

Extend ShareActionProvider (CustomShareActionProvider.java)

public class CustomShareActionProvider extends ShareActionProvider {

private ShareMenuListener mListener;

public CustomShareActionProvider(Context context) {
    super(context);
}

@Override
public void subUiVisibilityChanged(boolean isVisible) {
    super.subUiVisibilityChanged(isVisible);
    mListener.onMenuVisibilityChanged(isVisible);
}

public void setShareMenuListener(ShareMenuListener listener) {
    mListener = listener;
}
}

然后在你的活动或片段,用CustomShareActionProvider而不是标准ShareActionProvider和实施ShareMenuListener接口。实施onMenuVisibilityChanged方法来执行自定义操作:

Then in your activity or fragment, use CustomShareActionProvider instead of standard ShareActionProvider and implement ShareMenuListener interface. Implement onMenuVisibilityChanged method to perform custom action:

@Override
public void onMenuVisibilityChanged(boolean visible) {
    if(visible) {
        // Do your custom action here
    }
}

这篇关于如何在用户与ShareActionProvider选择菜单项检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆