好的,这是解决方案:添加新的侦听器接口(ShareMenuListener.java):
public interface ShareMenuListener {
public void onMenuVisibilityChanged(boolean visible);
}
扩展 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 方法以执行自定义操作:
@Override
public void onMenuVisibilityChanged(boolean visible) {
if(visible) {
// Do your custom action here
}
}