Chrome自定义选项卡。设置多个工具栏项目
我在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帮助我这最后一个问题,我有。
谢谢所有对解决此问题作出贡献的人!
您需要将不同的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);
}
启动了相同的意图,因为您使用相同的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。
感谢您的回复快,我已经编辑英里答案与建议。关于这个位置,我仍然有一个问题。我已经在我的编辑中更详细地解释过了。你可以看看吗? – JorgeMuci
感谢您的快速回复,我编辑了mi的答案和建议。关于这个位置,我仍然有一个问题。我已经在我的编辑中更详细地解释过了。你可以看看吗? – JorgeMuci
看看下面的文章。也许这有助于:http://*.com/questions/3127957/why-the-pendingintent-doesnt-send-back-my-custom-extras-setup-for-the-intent – andreban
真棒,正是我需要的。谢谢 – JorgeMuci