这个问题已经有答案了,但我想不是针对cardview的,我已经看了大部分的答案,并进行了实验,但我的问题还是没有解决,我有一个带适配器的recyclerview和cardview,现在我在cardview中添加了一个按钮,我想改变这个按钮的可见性,当我点击这个按钮时,它是不可见的。
我有一个带适配器的recyclerview和一个cardview,现在我在cardview中添加了一个按钮,我想把这个按钮的可见性改为当按钮被点击时不可见,但是当我这样做时,其他卡片中的其他按钮的可见性会受到影响,变得不可见,但我从未点击过第9张卡片上的按钮。
我找到了一些解决方案,比如在onBindViewHolder方法下写onClick。我这样做了,但它仍然没有工作,请帮助我。
以下是卡片视图的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/shop_cardview"
android:layout_width="match_parent"
android:layout_height="280dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
card_view:cardMaxElevation="5dp"
card_view:contentPadding="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/btnGrey">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/shopVolleyImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@mipmap/pyaa_logo_iii"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/shopImageNameTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="8dp"
android:text="Json Image Name"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20dp"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Button"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
以下是recyclerviewadapter的代码。
public class shopRecyclerViewAdapter extends RecyclerView.Adapter<shopRecyclerViewAdapter.ViewHolder> {
Context context;
List<shopDataAdapter> dataAdapters;
ImageLoader imageLoader;
public shopRecyclerViewAdapter(List<shopDataAdapter> getDataAdapter,Context context){
super();
this.dataAdapters=getDataAdapter;
this.context=context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_shop,parent,false);
ViewHolder vh=new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder Viewholder, int position) {
shopDataAdapter dataAdapterOBJ= dataAdapters.get(position);
imageLoader=shopImageAdapter.getInstance(context).getImageLoader();
imageLoader.get(dataAdapterOBJ.getshopImageUrl(),
ImageLoader.getImageListener(
Viewholder.VollyImageView,
R.mipmap.ic_launcher,
android.R.drawable.ic_dialog_alert
)
);
Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getshopImageUrl(),imageLoader);
Viewholder.ImageTitleTV.setText(dataAdapterOBJ.getshopName());
Viewholder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Viewholder.check.setVisibility(View.INVISIBLE);
}
});
}
@Override
public int getItemCount() {
return dataAdapters.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView ImageTitleTV;
public NetworkImageView VollyImageView;
public Button check;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleTV=itemView.findViewById(R.id.shopImageNameTV);
VollyImageView=itemView.findViewById(R.id.shopVolleyImageView);
check=itemView.findViewById(R.id.button);
}
}
}
你可以这样做声明全局变量clickPosition=-1。
public void onBindViewHolder(final ViewHolder Viewholder, final int position) {
Viewholder.check.setVisibility(View.VISIBLE);
Viewholder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickPosition = position;
notifyDataSetChanged();
}
});
if(clickPosition == position){
Viewholder.check.setVisibility(View.INVISIBLE);
}
}
不要忘了写Viewholder.check.setVisibility(View.VISIBLE)这一行,否则它会给出奇怪的行为。
如果你能给出更多的解释,你想要什么特定的行为,我可以给出更多的解释或确切的解决方案。