How to detect when user selects menu item with ShareActionProvider?

↘锁芯ラ 提交于 2019-12-11 09:41:03

问题


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.

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?


回答1:


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

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

}

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;
}
}

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
    }
}


来源:https://stackoverflow.com/questions/24187151/how-to-detect-when-user-selects-menu-item-with-shareactionprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!