从 RecyclerView 适配器启动 Chrome 自定义选项卡
Launching A Chrome Custom Tab from a RecyclerView Adapter
我打算像这样从 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();
}
}
});
}
由于 customTabsIntent.launchUrl
的方法签名要求第一个参数是 Activity
,我将上下文转换为 Activity 因此
java.lang.ClassCastException: android.app.Application cannot be cast
to android.app.Activity
上线customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
我该如何解决这个问题?
如果在回收视图中总是需要 activity,我认为获取 Context 并将其解析为 Activity 是没有意义的;而只是从构造函数中获取 Activity。
所以我建议将您的构造函数更改为
public CustomAdapter(Activity activity, List<Artifact> ListOfArtifacts) {
this.activity= activity;
this.ListOfArtifacts = ListOfArtifacts;
}
然后只需使用 activity 启动 Chrome custom tabs
。
customTabsIntent.launchUrl(activity, Uri.parse(ListOfArtifacts.get(position).getURL()));
如果您想知道您的代码有什么问题,我认为您传递的是应用程序上下文,而不是 Activity 上下文。
我打算像这样从 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();
}
}
});
}
由于 customTabsIntent.launchUrl
的方法签名要求第一个参数是 Activity
,我将上下文转换为 Activity 因此
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
上线customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
我该如何解决这个问题?
如果在回收视图中总是需要 activity,我认为获取 Context 并将其解析为 Activity 是没有意义的;而只是从构造函数中获取 Activity。
所以我建议将您的构造函数更改为
public CustomAdapter(Activity activity, List<Artifact> ListOfArtifacts) {
this.activity= activity;
this.ListOfArtifacts = ListOfArtifacts;
}
然后只需使用 activity 启动 Chrome custom tabs
。
customTabsIntent.launchUrl(activity, Uri.parse(ListOfArtifacts.get(position).getURL()));
如果您想知道您的代码有什么问题,我认为您传递的是应用程序上下文,而不是 Activity 上下文。