ShareActionProvider无法单击,并且在首次渲染时无法正确渲染 [英] ShareActionProvider not clickable and not rendering properly on first render

查看:161
本文介绍了ShareActionProvider无法单击,并且在首次渲染时无法正确渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ActionBar中有一个ShareActionProvider以及其他一些选项.但是,当首次以纵向模式渲染时,ShareActionProvider似乎无法正确渲染,并且在第一次渲染时不可单击.方向更改会重新渲染屏幕,然后所有应该显示的选项都可见,并且在向后旋转时,ActionBar会再次重新渲染,但这一次它也可以在纵向模式下正确渲染.

I have a ShareActionProvider together with some other options in my ActionBar. It seems however that the ShareActionProvider has problems rendering properly when first rendered in portrait mode and it is not clickable on the first render. An orientation change re-renders the screen and then all the options that are supposed to be visible are visible and when rotating back the ActionBar re-renders again but this time it is rendering properly in portrait mode as well.

我已附上一张描述情况的图片:

I've attached an image that describes the situation:

  1. 共享"选项未正确呈现,它旁边应该有一个图标,如果它不适合布局,则应成为一个三点菜单.

  1. The Share option is not properly rendered, it should have an icon next to it and if it doesn't fit the layout, it should become a 3-dot menu.

定位后,所有选项均按预期显示.

After orientation all options are visible as expected.

旋转回纵向可重新渲染ActionBar,现在将按需显示3点菜单,并且该菜单是可单击的.

Rotating back to portrait re-renders the ActionBar and now the 3-dot menu appear as it should and it is clickable.

关于这里发生的事情有什么想法吗?

Any ideas on what's going on here?

这是我的XML:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/item_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:visible="false"/>
    <item
        android:id="@+id/item_edit"
        android:icon="@android:drawable/ic_menu_edit"
        android:showAsAction="ifRoom|withText"
        android:title="Edit"
        android:visible="false"/>
    <item
        android:id="@+id/item_share"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom|withText"
        android:title="Share"
        android:visible="false"/>
    <item
        android:id="@+id/item_save"
        android:icon="@android:drawable/ic_menu_save"
        android:showAsAction="ifRoom|withText"
        android:title="Save"
        android:visible="false"/>
    <item
        android:id="@+id/menu_search"
        android:actionViewClass="android.widget.SearchView"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom"
        android:title="@string/menu_search"
        android:visible="false"/>

</menu>

这就是我处理片段中的选项菜单的方式:

and this is how I handle the options menu in a Fragment:

 /**
 * Hook into the OptionsMenu and add an Edit, Delete and Share option.
 */
@Override
public void onPrepareOptionsMenu(Menu menu) {
    MenuItem deleteItem = menu.findItem(R.id.item_delete);
    deleteItem.setVisible(true);

    MenuItem editItem = menu.findItem(R.id.item_edit);
    editItem.setVisible(true);

    MenuItem shareItem = menu.findItem(R.id.item_share);
    shareItem.setVisible(true);
    shareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
    shareActionProvider.setShareIntent(getShareIntent());

    super.onPrepareOptionsMenu(menu);
}

/**
 * Builds an intent that takes the path for the image and passes it to 
 * the sharing mechanism as a stream built on the URI of the image path.
 * @return the intent to share the image as a stream
 */
private Intent getShareIntent()
{
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + receipt.getPhoto()));
    shareIntent.setType("image/jpeg");
    return shareIntent;
}

推荐答案

这是因为您必须在onCreateOptionsMenu上的菜单充气之后立即向ShareActionPRovider添加意图.

That's because you have to add an intent to the ShareActionPRovider right after you inflate the menu, on onCreateOptionsMenu.

如果仅在onPrepareOptionsMenu中执行此操作,则必须手动调用invalidateOptionsMenu()来触发ActionBar更新(如所选答案所提示).但这不是做到这一点的方法.

If you do that only in onPrepareOptionsMenu, you'll have to manually call invalidateOptionsMenu() to trigger an ActionBar update (as the chosen answer tells you to). But that's not the way to do it.

当配置更改时,它可以正常工作,因为调用了onPrepareOptionsMenu(),然后您的共享按钮将起作用,因为它现在具有一个Intent.

It works fine when the configuration changes, because onPrepareOptionsMenu() is called, and then your share button will work, since it now has an Intent.

要解决此问题,只需执行以下操作即可:

To fix that, just do something like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.YOUR_MENU_XML, menu);

    ShareActionProvider provider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();

    if (provider != null) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, YOUR_TEXT);
        shareIntent.setType("text/plain");
        provider.setShareIntent(shareIntent);
    }

    return true;
}

通过这种方式,ShareActionProvider从一开始就会有一个Intent,并且会按预期工作.

This way, the ShareActionProvider will have an Intent from the beginning, and will work as expected.

这篇关于ShareActionProvider无法单击,并且在首次渲染时无法正确渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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