简体   繁体   English

从RecyclerView适配器启动Chrome自定义标签

[英]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.

相关问题 从 RecyclerViewAdapter 启动BottomSheet,它在onBindViewHolder 内扩展了RecyclerView.Adapter - Launching BottomSheet from a RecyclerViewAdapter that extends with RecyclerView.Adapter inside onBindViewHolder 从适配器启动 DialogFragment - Launching DialogFragment from Adapter Android从中创建自定义RecyclerView.Adapter并创建其他类 - Android Create a Custom RecyclerView.Adapter and Create Other Class from it 从适配器单击第一个选项卡recyclerview项时,如何为第二个选项卡设置动画? - how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter? 使用自定义适配器重新订购Recyclerview项目 - Reorder Recyclerview items with custom adapter 从适配器中的onclick刷新recyclerview - refreshing recyclerview from the onclick in the adapter 从RecyclerView适配器启动Fragment - Start Fragment from RecyclerView adapter 如何从同一片段中的其他 recyclerview 适配器刷新 recyclerview 适配器 - How to refresh recyclerview adapter from other recyclerview adapter in same fragment 自定义适配器内的Android Refresh RecyclerView - Android Refresh RecyclerView Inside Custom Adapter 使用列表为我的 RecyclerView 制作自定义适配器 - Made a custom Adapter for my RecyclerView with a List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM