【问题标题】:Recycler view focusing another text view in other rows回收站视图聚焦其他行中的另一个文本视图
【发布时间】:2015-03-02 14:47:45
【问题描述】:

在我的项目中,我有一个回收站视图,其中每一行都包含一个文本视图。当我在一行中选择一个文本视图时,一些随机行中的文本视图也被选中,并且无论输入什么,它也显示在这些行中。这是我的代码。 onBindViewHolder

public void onBindViewHolder(final MainContentViewHolder holder, int position) {
    Log.e("Sony","onBindViewHolder");
    Item currentItem = items.get(position);
    if (currentItem.imageURL != null) {
        imageLoader.get(currentItem.imageURL, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                Log.e("Sony","onResponseImage");
                holder.itemImage.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Sony","onErrorImage");
                holder.itemImage.setImageResource(R.drawable.default_product);
            }
        });
    }
    //holder.itemImage.setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL().getContent());

    holder.itemPrice.setText(currentItem.price + "");
    holder.itemName.setText(currentItem.itemName);
}

ViewHolder

 public MainContentViewHolder(View itemView) {
        super(itemView);

        itemImage = (ImageView) itemView.findViewById(R.id.imgItemPic);
        itemName = (TextView) itemView.findViewById(R.id.lblItemName);
        itemPrice = (TextView) itemView.findViewById(R.id.lblItemPrice);
        txtQty = (EditText) itemView.findViewById(R.id.txtQty);
        btnAddToCart = (Button) itemView.findViewById(R.id.btnAddToCart);
        btnAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Item item = items.get(getPosition());
                CartMessageHandler.showToastMessage(context, item.itemName + " : " + item.price, Toast.LENGTH_LONG);
            }
        });
    }

创建活动

 mainListView = (RecyclerView) findViewById(R.id.recyclerList);
    mainListView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    recyclerViewListAdapter = new MainContentRecyclerAdapter(this);
    mainListView.setAdapter(recyclerViewListAdapter);
    getData("fruits%20&%20vegetables");
    mainListView.setLayoutManager(new LinearLayoutManager(this));

recyclerView 的自定义行

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardItemHolderRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/imgItemPic"
    android:layout_width="@dimen/itemImageDimen"
    android:layout_height="@dimen/itemImageDimen"
    android:layout_centerVertical="true"
    android:contentDescription="@string/productImage"
    android:src="@drawable/default_product"
    android:elevation="2dp"
    android:focusableInTouchMode="false"
    android:layout_marginTop="@dimen/loginWidgetTopMargin" />
<LinearLayout
    android:layout_toRightOf="@+id/imgItemPic"
    android:paddingLeft="10dp"
    android:id="@+id/cardItemHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lblItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/loginWidgetTopMargin"
        android:text="Item  Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/lblItemPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/loginWidgetTopMargin"
        android:text="Rs.250"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/primaryColor" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/loginWidgetTopMargin"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/lblItemQty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/quantity"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/txtQty"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

    </LinearLayout>

    <Button
        android:id="@+id/btnAddToCart"
        android:layout_width="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/cart_a16"
        android:text="@string/addToCart" />
</LinearLayout>

我的代码有什么问题?任何人都可以提出解决此问题的方法

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    RecyclerView 重用之前创建的行视图。这就是为什么您会在 RecyclerView 的随机行中看到之前键入的文本:您根本不会在 onBindViewHolder 中重置行视图的内容状态。

    因此,为了避免错误,您应该在 onBindViewHolder 中重置组件状态:清除 txtQty 等。如果出现昂贵或重量级的数据文档,建议使用 @987654321 发布此数据@。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多