【问题标题】:How to change textview text on cardview click?如何更改cardview点击上的textview文本?
【发布时间】:2019-09-30 20:23:51
【问题描述】:

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

卡片视图由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" />

这里是卡片视图的点击,我得到了卡片视图的位置,但是不知道如何获取卡片视图里面的textview。

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);
                        }
                    }
                }
            });
        }
    }

【问题讨论】:

  • 在卡片视图点击事件期间为要更改的 TextView 添加一个 id。然后 setOnclickListener 到 CardView,在 clicklistener 的回调上获取 TextView 的引用,如果它存在的话,给它设置一个新的文本。很简单。
  • @CodeDaily 我可以得到卡片视图的位置,但仍然不知道如何获取文本视图:/
  • 我明白了,你在使用适配器吗?
  • 发布适配器,我可以帮助你。
  • @CodeDaily 我已经用适配器的一部分更新了问题(查看 HoldeR)如果您需要查看所有源代码,请告诉我

标签: android android-cardview cardview


【解决方案1】:

你必须重写 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());
      }
    });
  }
}

【讨论】:

  • 感谢您的帮助,但我真的让我的 onBindViewHolder 感到困惑,看起来与您所拥有的不同:/ 抱歉在 android 上没有达到您的水平...我真的是新人跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多