如何更改cardview单击上的textview文本?

时间:2019-05-13 20:52:44

标签: android android-cardview cardview

在卡片视图中,我有一个图像视图和两个文本视图。我在尝试弄清当有人单击cardview时如何更改textview之一的文本时遇到问题。这甚至可能吗?

卡片视图由recyclerview控制。总共我必须有20张卡片视图。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardCornerRadius="5dp"
    android:foreground="?android:attr/selectableItemBackground">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:background="@color/transparent"
        android:padding="0dp"
        android:scaleType="fitCenter"
        android:src="@drawable/card1" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:text="Text Here"
        android:textColor="@color/red" />

    <TextView
        android:id="@+id/product"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="SUV"
        android:textColor="@color/grey_font"
        android:textSize="11dp" />

这是点击卡片视图,我得到了卡片视图的一部分,但不知道如何在卡片视图内获取文本视图。

public void onItemClick(int position) {
   Log.e(TAG, "Bien: " + position);
}

public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
    Items  currentItem = vReyclerViewList.get(position);

    String heading      = currentItem.getHeading();
    String title        = currentItem.getTitlee(); 

    holder.vHeading.setText(heading);
    holder.vTitle.setText(title);
}



public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public TextView product; 

        public RecyclerViewHolder(View itemView) {
            super (itemView);
            title       = itemView.findViewById(R.id.title);
            product         = itemView.findViewById(R.id.product); 

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(vListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            vListener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:3)

您必须重写onBindViewHolder()方法,然后执行以下操作。

Override 
public void  onBindViewHolder(RecyclerViewHolder holder, int position) {
   Model model = getItem(position);
   holder.bind(model);
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

  public final TextView title;
  public final TextView product;

  public RecyclerViewHolder(View itemView) {
    super (itemView);
    title    = itemView.findViewById(R.id.title);
    product  = itemView.findViewById(R.id.product);
  }

  public void bind(final Model model){
    itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
       title.setText(model.getText());
      }
    });
  }
}