Android ShareActionProvider with popup menu - undesired duplicate list

匆匆过客 提交于 2020-01-02 07:47:25

问题


Okay this is a pretty specific one: My ShareActionProvider is being used on posts in a forum. It works (apart from pesky facebook but I understand that is a well-known issue). However, when I select the share option from my pop-up menu, two lists are drawn, one on top of the other.

How can I fix it so only one list is displayed?

Edit: At least two other questions exist on SO referring to this problem:

I Use ShareActionProvider in PopupMenu, but show two PopupMenu?

Call ShareActionProvider from a PopupMenu

Here is my code: Menu item in xml

    <item
    android:id="@+id/menu_community_overflow_share"
    android:title="@string/menu_share"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:orderInCategory="1"
    app:showAsAction="never" />

Java

    private void share(MenuItem item) {
    mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item);

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Here's a message originally posted by " + mClickedMessage.getFirstName()
            + " " + mClickedMessage.getLastName() + ": " + mClickedMessage.getTheMessage() + "\n\n");
    sendIntent.setType("text/plain");

    setShareIntent(sendIntent);
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
}

And here are some images: Share list is first created

When See All is clicked

When list is scrolled

Hopefully the problem is clear. Also note the icons cannot be clicked, doing so behaves like a click outside the list all together and kills it.


回答1:


Here is the code I eventually went with (I'm not even sure how I figured it out as it was a long time ago), but it might help someone.

The solution was not ideal, it removes the duplicate list, but leaves behind the boring list (without icons), and I kind of wanted the other one. If I recall this method of sharing is outdated and it would be excellent if someone could shed more light on the issue.

private void share(MenuItem item) {
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Here's a message originally posted by " + mClickedMessage.getFirstName()
                + " " + mClickedMessage.getLastName() + ": " + mClickedMessage.getTheMessage() + "\n\n Sent via Loylap");
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_post_to)));
    }



回答2:


So I came up with my own solution that basically replaces ShareActionProvider with a new class that gets the same Activites with the same intent filter, called ShareActionAdapter. The relevant snippet is as follows though:

 Intent intent = new Intent(Intent.ACTION_SEND);
 intent.setType("text/plain");
 PackageManager pm = context.getPackageManager();
 m_list = new ArrayList<>(pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER));

The full gist that mimics a the popup experience can be found here



来源:https://stackoverflow.com/questions/22665226/android-shareactionprovider-with-popup-menu-undesired-duplicate-list

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