单击特定卡片视图时打开特定 activity
Opening specific activity when clicking on a specific cardview
我有 5 个卡片视图,每个卡片视图都从我的 Firebase 项目中加载了数据。我希望他们每个人都打开一个特定的 activity 但是当我随机点击一个时,所有 5 个活动都会开始。
所以我的问题是:如何在单击特定卡片视图时打开特定 Activity?
我一直在关注这个答案
我的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;
}
}
});
}
还有我的classViewHolder
:
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);
}
}
我的卡片视图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>
请帮忙,提前致谢。
您只需要为每个 switch
案例添加 break
语句;当你得到一个没有 break
语句的 case
匹配时,那么所有底层 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;
}
我有 5 个卡片视图,每个卡片视图都从我的 Firebase 项目中加载了数据。我希望他们每个人都打开一个特定的 activity 但是当我随机点击一个时,所有 5 个活动都会开始。
所以我的问题是:如何在单击特定卡片视图时打开特定 Activity?
我一直在关注这个答案
我的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;
}
}
});
}
还有我的classViewHolder
:
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);
}
}
我的卡片视图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>
请帮忙,提前致谢。
您只需要为每个 switch
案例添加 break
语句;当你得到一个没有 break
语句的 case
匹配时,那么所有底层 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;
}