2012-08-23 40 views
9

Tôi có một ShareActionProvider cùng với một số tùy chọn khác trong ActionBar của mình. Tuy nhiên có vẻ như ShareActionProvider có vấn đề hiển thị đúng khi lần đầu tiên được hiển thị ở chế độ dọc và không thể nhấp vào lần hiển thị đầu tiên. Một thay đổi định hướng tái hiện màn hình và sau đó tất cả các tùy chọn được cho là hiển thị có thể nhìn thấy và khi quay lại ActionBar lại hiển thị lại nhưng lần này nó cũng hiển thị đúng ở chế độ dọc.ShareActionProvider không thể nhấp và không hiển thị đúng trên lần hiển thị đầu tiên

Tôi đã kèm theo một hình ảnh mô tả tình hình:

  1. Tùy chọn chia sẻ không trả lại đúng, nó cần phải có một biểu tượng bên cạnh nó và nếu nó không phù hợp với bố trí, nó nên trở thành menu 3 dấu chấm.

  2. Sau khi định hướng tất cả các tùy chọn hiển thị như mong đợi.

  3. Quay trở lại chân dung để hiển thị lại ActionBar và bây giờ menu 3 dấu chấm xuất hiện khi cần và có thể nhấp.

Mọi ý tưởng về những gì đang diễn ra ở đây?

ActionBar description

Đây là XML của tôi:

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

và đây là cách tôi xử lý menu tùy chọn trong một 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; 
} 

Trả lời

28

Đó là vì bạn phải thêm mục đích vào ShareActionPRovider ngay sau khi bạn tăng cấp độ cho menu, trên onCreateOptionsMenu.

Nếu bạn làm điều đó chỉ trong onPrepareOptionsMenu, bạn sẽ phải gọi thủ công invalidateOptionsMenu() để kích hoạt cập nhật ActionBar (như câu trả lời đã chọn cho bạn biết). Nhưng đó không phải là cách để làm điều đó.

Nó hoạt động tốt khi thay đổi cấu hình, bởi vì onPrepareOptionsMenu() được gọi và sau đó nút chia sẻ của bạn sẽ hoạt động, vì giờ đây nút này có Mục đích.

Để khắc phục điều đó, chỉ cần làm một cái gì đó như thế này:

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

Bằng cách này, ShareActionProvider sẽ có một Intent từ đầu, và sẽ làm việc như mong đợi.

+0

Rất thú vị! Tôi sẽ xem xét điều này ngay sau khi tôi nhận được một số thời gian rảnh rỗi. Cảm ơn! – span

1

Vì vậy, có vẻ như gọi "getActivity () .invalidateOptionsMenu() "trong onCreateView trong phần Tạo ảnh s trình đơn tái render như nó cần. Nó sẽ hiển thị đúng trong lần chạy đầu tiên, làm mất hiệu lực trình đơn mà không thực hiện các thay đổi đối với nó không giống như một giải pháp thích hợp.

1

Tôi không nghĩ đó là lỗi. Đó là vì tiêu đề của bạn đã thay đổi. Nó ngắn hơn ("Chi tiết" thay vì "ReceiptDetail") ban đầu, và vì vậy hệ thống phải nghĩ rằng có nhiều chỗ hơn để hiển thị nhiều mục hành động hơn.

Ngoài ra chiều rộng của ShareActionProvider là động (có thể lên đến hơn 2 lần chiều rộng bình thường).

Để kiểm tra một hoặc hai điều, tôi sẽ đề xuất bạn di chuyển mục tác vụ chia sẻ lên vị trí đầu tiên, xóa giải pháp tạm thời của bạn và xem liệu điều đó vẫn xảy ra. Bạn cũng có thể xóa mục hành động chia sẻ và sử dụng 3 hoặc nhiều mục tác vụ thông thường làm thử nghiệm.

+0

Xin cảm ơn, sẽ xem xét điều này khi tôi trở về từ kỳ nghỉ của tôi. – span

Các vấn đề liên quan