我打算从RecyclerView
这样发动Chrome Custom
标签 -从RecyclerView适配器启动Chrome的自定义标签
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
,我铸历境进入一个活动因此
java.lang.ClassCastException:android.app.Application不能转换到 android.app.Activity
就行customTabsIntent.launchUrl((Activity) context, Uri.parse(ListOfArtifacts.get(position).getURL()));
我该如何解决这个问题?
目前尚不清楚你从哪里获得上下文。如果您从中获得上下文,请添加构造函数。 – humazed
@humazed - 添加了构造函数 – CodeWalker
您还可以添加设置适配器的位置。 – humazed