android - 单击特定卡片 View 时打开特定 Activity

标签 android android-activity cardview

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

所以我的问题是:单击特定卡片 View 时如何打开特定 Activity ?

我一直在关注这个答案 here

我的 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;
}

关于android - 单击特定卡片 View 时打开特定 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61602281/

相关文章:

android - 如何在与父级底部对齐的 CardView 上添加阴影

android - 为什么我的 Exoplayer2 CacheDataSource 没有将下载的流写入指定的缓存?

Android:在 Activity 屏幕方向更新期间保持 MediaPlayer 运行

java - android studio 中选项卡/Activity 的问题

android - ImageView onClick 需要点击两次才能执行操作

java - 如何在RecyclerView上设置onClickListener?

android - 如何在父 fragment 简历上恢复子 fragment

android - 从保存位置检索文件时出现 NullPointerException

android - 布局中的 TabHost NullPointerException

GWT MVP 在不更改 Activity 的情况下更新 History-Token