2016-04-15 140 views
2

我在Android應用上實施Chrome自定義選項卡(使用最新版本23.3.0)。最新版本的chrome選項卡允許您使用「builder.addToolbarItem()」方法(根據此stack overflow answer其他可定製的東西)添加底部工具欄上的按鈕。現在,當爲我的底部工具欄按鈕添加動作意圖。我爲每個添加的工具欄項目設置了兩個不同的動作意圖,但是當打開Chrome自定義選項卡並單擊我添加的任何工具欄項目時,都會啓動相同的意圖,啓動的意圖始終對應於在第一欄項意圖組加入。Chrome自定義選項卡。設置多個工具欄項目

這裏是在回收視圖的項目,當點擊我用它來生成自定義選項卡中的代碼。

protected void openCustomTab(Listing listing, int position) { 
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); 
    builder.setToolbarColor(ContextCompat.getColor(this, R.color.primary)); 
    builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide_out_left); 
    builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_out_right); 
    builder.setCloseButtonIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back)); 

    addShareAction(listing, builder); 
    setBottomToolbar(listing, position, builder); 
    CustomTabActivityHelper.openCustomTab(
      this, builder.build(), Uri.parse(listing.getListingURL())); 
} 

private void setBottomToolbar(Listing listing, int position, CustomTabsIntent.Builder builder) { 
    builder.setSecondaryToolbarColor(ContextCompat.getColor(this, R.color.color_bottom_toolbar)); 
    addFavoriteButton(listing, position, builder); 
    addReportButton(position, builder); 
} 

private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) { 
    Bitmap iconShare = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_share_custom_tab); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType("text/plain"); 
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message)); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL()); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, shareIntent, 0); 
    builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true); 
} 

private void addReportButton(int position, CustomTabsIntent.Builder builder) { 
    Bitmap reportIcon = BitmapFactory.decodeResource(getResources(), 
      R.drawable.detail_bottom_hide); 
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left, 
      R.anim.slide_out_right).toBundle(); 
        Intent reportIntent = createDetailActionIntent(position, ViewsConstants.REPORT); 
    PendingIntent pi = PendingIntent.getActivity(this, ACTION_REPORT, reportIntent, 0, menuBundle); 

    builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi); 
} 

private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) { 
    Bitmap favIcon; 
    if (listing.getIsFavorite()) { 
     favIcon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.detailbottom_fav); 
    } else { 
     favIcon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.detailbottom_nofav); 
    } 
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left, 
      android.R.anim.slide_out_right).toBundle(); 
    Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE); 
    PendingIntent pi = PendingIntent.getActivity(this, 0,favIntent, 0, menuBundle); 
    builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi); 
} 

private Intent createDetailActionIntent(int position, String action) { 
    Intent actionIntent = new Intent(getApplicationContext(), ListingActivity.class); 
    actionIntent.putExtra(ViewsConstants.SEARCH_PARAMETERS, getListingPresenter().getSearchParameters()); 
    actionIntent.putExtra(ViewsConstants.POSITION, position); 
    actionIntent.putExtra(ViewsConstants.DETAIL_ACTION, action); 
    return actionIntent; 
} 

所以在運行這段代碼之後,得到一個帶有底部工具欄和兩個按鈕最喜歡和報告的chrome自定義選項卡。點擊任何按鈕將始終啓動最喜歡的動作..我已經確保通過多次調試代碼正確地將值傳遞給意圖。 我不知道我在這裏錯過了什麼。我開始認爲這可能是Chrome自定義選項卡上的一個錯誤,但可能有一些我錯過了。任何幫助將不勝感激。提前致謝。

EDITED

我已經編輯給出建議的代碼基礎,現在它工作正常有關行動,爲每個按鈕執行。比你! 但我仍然有一個問題,關於位置。我正在設置在意圖上選擇的項目的位置,但是當打開鉻標籤時,單擊任何操作按鈕並返回活動,意圖只設置了操作,但位置丟失。我不明白爲什麼?我在上面代碼中顯示的方法createDetailActionIntent()中設置所有intent值。

任何想法,爲什麼從鉻自定義選項卡回到活動,並檢索意向額外時,位置丟失???

post幫助我這最後一個問題,我有。

謝謝所有對解決此問題作出貢獻的人!

回答

3

您需要將不同的requestCode傳遞給PendingIntent.getActivity調用,否則將覆蓋PendingIntent。查看PendingIntent docs瞭解更多詳情。

自定義標籤Github演示有code,實現這個正確的方式。

您的代碼應該如下所示:

private static final int ACTION_CODE_REPORT = 1; 
private static final int ACTION_CODE_SHARE = 2; 

private void addShareAction(Listing listing, CustomTabsIntent.Builder builder) { 
    Bitmap iconShare = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_share_custom_tab); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.setType("text/plain"); 
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_item_message)); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, listing.getShareURL()); 
    PendingIntent pi = PendingIntent.getActivity(this, ACTION_CODE_SHARE, shareIntent, 0); 
    builder.setActionButton(iconShare, getString(R.string.share_item_message), pi, true); 
} 

private void addReportButton(int position, CustomTabsIntent.Builder builder) { 
    Bitmap reportIcon = BitmapFactory.decodeResource(getResources(), 
      R.drawable.detail_bottom_hide); 
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, R.anim.slide_in_left, 
      R.anim.slide_out_right).toBundle(); 
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), ACTION_CODE_REPORT, 
      createDetailActionIntent(position, ViewsConstants.REPORT), 0, menuBundle); 
    builder.addToolbarItem(2, reportIcon, getString(R.string.report_listing_dialog_title), pi); 
} 
+0

感謝您的快速回復,我編輯了mi的答案和建議。關於這個位置,我仍然有一個問題。我已經在我的編輯中更詳細地解釋過了。你可以看看嗎? – JorgeMuci

+1

看看下面的文章。也許這有助於:http://stackoverflow.com/questions/3127957/why-the-pendingintent-doesnt-send-back-my-custom-extras-setup-for-the-intent – andreban

+0

真棒,正是我需要的。謝謝 – JorgeMuci

2

啓動了相同的意圖,因爲您使用相同的Intent動作和相同的請求代碼創建了兩個PendingIntent

解決您的問題改變PendingIntent.getActivity請求代碼addFavoriteButton

private void addFavoriteButton(Listing listing, int position, CustomTabsIntent.Builder builder) { 
    Bitmap favIcon; 
    if (listing.getIsFavorite()) { 
     favIcon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.detailbottom_fav); 
    } else { 
     favIcon = BitmapFactory.decodeResource(getResources(), 
       R.drawable.detailbottom_nofav); 
    } 
    Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.anim.slide_in_left, 
      android.R.anim.slide_out_right).toBundle(); 
    Intent favIntent = createDetailActionIntent(position, ViewsConstants.FAVORITE); 
    PendingIntent pi = PendingIntent.getActivity(this, 1,favIntent, 0, menuBundle); 
    builder.addToolbarItem(1, favIcon, getString(R.string.add_favorite), pi); 
} 

如果您想了解更多關於如何PendingIntent運作的,你可以閱讀這個StackOveflow answer

+0

感謝您的回覆快,我已經編輯英里答案與建議。關於這個位置,我仍然有一個問題。我已經在我的編輯中更詳細地解釋過了。你可以看看嗎? – JorgeMuci