单击特定卡片视图时打开特定活动

丹尼特兰

我有 5 个卡片视图,每个卡片视图都从我的 Firebase 项目加载了它们的数据。我希望他们每个人都打开一个特定的活动,但是当我随机单击一个活动时,所有 5 个活动都会开始。

所以我的问题是:单击特定卡片视图时如何打开特定活动?

我一直在这里关注这个答案

我的onBindViewHolder()

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
    String strTreatmentImage = treatmentList.get(position).getImage();
    Picasso.get().load(strTreatmentImage).placeholder(R.drawable.ic_circle).into(viewHolder.treatmentImage);

    String strTreatmentName = treatmentList.get(position).getTreatmentName();
    viewHolder.treatmentName.setText(strTreatmentName);

    viewHolder.treatmentCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent;
            switch (position) {
                case 0:
                    intent = new Intent(context, LashLiftActivity.class);
                    context.startActivity(intent);
                case 1:
                    intent = new Intent(context, BrowsActivity.class);
                    context.startActivity(intent);
                case 2:
                    intent = new Intent(context, NiMesoActivity.class);
                    context.startActivity(intent);
                case 3:
                    intent = new Intent(context, MesoTherapyActivity.class);
                    context.startActivity(intent);
                case 4:
                    intent = new Intent(context, SingleLashesActivity.class);
                    context.startActivity(intent);
                    break;
            }
        }
    });

}

我的班级ViewHolder

static class ViewHolder extends RecyclerView.ViewHolder{

    ImageView treatmentImage;
    TextView treatmentName;
    CardView treatmentCardView;

    ViewHolder(@NonNull View itemView) {
        super(itemView);

        treatmentImage = itemView.findViewById(R.id.treatmentThumb);
        treatmentName = itemView.findViewById(R.id.txt_treatmentName);
        treatmentCardView = itemView.findViewById(R.id.treatmentCardView);

        ButterKnife.bind(this, itemView);
    }
}

我的 CardView xml:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:id="@+id/treatmentCardView">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:divider="@null"
    android:dividerHeight="0dip">

    <ImageView
        android:id="@+id/treatmentThumb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:src="@drawable/logo_5"/>

    <TextView
        android:id="@+id/txt_treatmentName"
        android:gravity="center"
        android:textSize="24sp"
        android:text="Treatment name #1"
        android:layout_marginBottom="8dp"
        android:fontFamily="@font/kollektif"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

请帮助,并提前致谢。

赞恩

您只需要break为每个switch案例添加语句当您得到一个case没有break语句匹配项时,所有底层case语句仍将执行。

switch (position) {
    case 0:
        intent = new Intent(context, LashLiftActivity.class);
        context.startActivity(intent);
        break;
    case 1:
        intent = new Intent(context, BrowsActivity.class);
        context.startActivity(intent);
        break;
    case 2:
        intent = new Intent(context, NiMesoActivity.class);
        context.startActivity(intent);
        break;
    case 3:
        intent = new Intent(context, MesoTherapyActivity.class);
        context.startActivity(intent);
        break;
    case 4:
        intent = new Intent(context, SingleLashesActivity.class);
        context.startActivity(intent);
        break;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在单击按钮时打开特定的选项卡式活动?

如何通过单击外部链接打开特定活动

如何在点击链接时打开特定活动?

如何在点击推送通知时打开特定活动?

当来自 firebase 的推送通知时打开特定活动

通过不同的活动在RecycleAdapter中打开特定的卡片项目

单击通知时如何打开特定片段

如何通过单击外部链接来打开特定ID的特定活动

如何使用OneSignal打开特定活动?

如何基于通知打开特定活动

点击外部链接,如何打开特定应用的特定活动?

如何在“小部件/今日扩展”上打开特定视图控制器,请单击

Jstree在加载时打开特定分支

SwiftUI-用户打开推送通知时打开特定视图

使用 React 钩子从卡片列表中打开特定卡片的模式

单击主屏幕小部件时如何打开特定片段

单击小型设备的子元素时打开特定的下拉菜单

从外部组件单击按钮时,在一个组件中打开特定的选项卡

单击推送通知时如何在Flutter应用中打开特定页面

Swift - 收到推送通知时如何打开特定的视图控制器?

点击本地通知时打开特定的视图控制器

用户设置警报后,如何强制打开特定活动?

应用未运行时打开特定的活动

从命令行打开特定的活动

如何从onClick的其他活动中打开特定片段

如何通过另一个活动按钮单击事件在viewpager中打开特定片段页面

如何从AppDelegate打开特定的选项卡视图

如何使用主页快速操作打开特定视图

打开特定文件时破坏python代码