回收者视图将另一个文本视图聚焦在其他行中
Recycler view focusing another text view in other rows
在我的项目中,我有一个回收站视图,其中每一行都包含一个文本视图。当我 select 一行中的文本视图时,某些随机行中的文本视图也被 selected 以及输入其中的任何内容,它也显示在那些 rows.Here 中是我的代码。
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);
}
});
}
activity onCreate
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>
我的代码有什么问题?谁能提出解决此问题的方法
RecyclerView 重复使用之前创建的行视图。这就是为什么您在 RecyclerView 的随机行中看到较早键入的文本:您只是不在 onBindViewHolder.
中重置行视图内容状态
因此,为了避免错误,您应该在 onBindViewHolder 中重置组件状态:清除 txtQty 等。在昂贵或重量级数据文档的情况下建议使用 setRecyclerListener(..).
发布此数据
在我的项目中,我有一个回收站视图,其中每一行都包含一个文本视图。当我 select 一行中的文本视图时,某些随机行中的文本视图也被 selected 以及输入其中的任何内容,它也显示在那些 rows.Here 中是我的代码。 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);
}
});
}
activity onCreate
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>
我的代码有什么问题?谁能提出解决此问题的方法
RecyclerView 重复使用之前创建的行视图。这就是为什么您在 RecyclerView 的随机行中看到较早键入的文本:您只是不在 onBindViewHolder.
中重置行视图内容状态因此,为了避免错误,您应该在 onBindViewHolder 中重置组件状态:清除 txtQty 等。在昂贵或重量级数据文档的情况下建议使用 setRecyclerListener(..).
发布此数据