[英]Launching A Chrome Custom Tab from a RecyclerView Adapter
I intend to launch a Chrome Custom
Tab from the RecyclerView
like this - 我打算像这样从RecyclerView
启动Chrome Custom
标签-
public CustomAdapter(Context context, List<Artifact> ListOfArtifacts) {
this.context = context;
this.ListOfArtifacts = ListOfArtifacts;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.artifactAuthor.setText(ListOfArtifacts.get(position).getAuthor());
holder.artifactTitle.setText(ListOfArtifacts.get(position).getTitle());
holder.seeders.setText(String.valueOf(ListOfArtifacts.get(position).getSeeders()));
holder.leechers.setText(String.valueOf(ListOfArtifacts.get(position).getLeechers()));
holder.addedOn.setText(df.format(ListOfArtifacts.get(position).getAdded_on()));
holder.artifactTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
/*Intent launchArtifactAuthor = new Intent(Intent.parseUri(ListOfArtifacts.get(position).getURL(), Intent.URI_INTENT_SCHEME));
context.startActivity(launchArtifactAuthor);*/
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
// Begin customizing
// set toolbar colors
intentBuilder.setToolbarColor(ContextCompat.getColor(context, R.color.colorPrimary));
intentBuilder.setSecondaryToolbarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
// set start and exit animations
intentBuilder.setStartAnimations(context, android.R.anim.slide_out_right, android.R.anim.fade_in);
intentBuilder.setExitAnimations(context, android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
// build custom tabs intent
CustomTabsIntent customTabsIntent = intentBuilder.build();
customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
Since the method signature of customTabsIntent.launchUrl
requires the first parameter to be an Activity
, I cast the context into a Activity hence the 由于customTabsIntent.launchUrl
的方法签名要求第一个参数为Activity
,因此我将上下文转换为Activity,因此
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity java.lang.ClassCastException:android.app.Application无法转换为android.app.Activity
on the line customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
在线customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
How do I fix this ? 我该如何解决 ?
If you will always need activity inside your recycle view, I think there is no point in getting Context and parsing it to Activity; 如果您在回收视图中始终需要活动,那么我认为获取Context并将其解析为Activity是没有意义的。 instead just get the Activity from the constructor. 相反,只需从构造函数中获取Activity。
so I suggest changing your constructor to 所以我建议将您的构造函数更改为
public CustomAdapter(Activity activity, List<Artifact> ListOfArtifacts) {
this.activity= activity;
this.ListOfArtifacts = ListOfArtifacts;
}
and just use activity to launch Chrome custom tabs
. 并仅使用活动启动Chrome custom tabs
。
customTabsIntent.launchUrl(activity, Uri.parse(ListOfArtifacts.get(position).getURL()));
in case you are wondering what's wrong with your code, I think you are passing Application context, not Activity context. 如果您想知道代码有什么问题,我认为您正在传递应用程序上下文,而不是活动上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.