单击项目其他项目上的按钮Xamarin RecyclerView

时间:2016-10-07 09:49:49

标签: android xamarin android-recyclerview

我有一个Recyclerview,其中的项目有一些隐藏的额外描述。

我按了一个按钮来显示或隐藏额外的描述。

它运作正常,但问题在于,如果我按下第一项,当我滚动时我会看到其他一些项目也被显示出来(例如,如果我按下第一项,它也会显示7,10,17和20)。

当我向后滚动时,显示的项目再次被隐藏,这并不会让我感到烦恼,但它会提供任何信息。

这是适配器的代码

class ArticulosAdapter : RecyclerView.Adapter
{
    // Event handler for item clicks:
    public event EventHandler<int> ItemClick;

    // Underlying data set 
    public List<Articulo> listado;


    public ArticulosAdapter(List<Articulo> listadoArticulos)
    {
        listado = listadoArticulos;
    }

    // Create a new CardView (invoked by the layout manager): 
    public override ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        // Inflate the CardView for the photo:
        View itemView = LayoutInflater.From(parent.Context).
                    Inflate(Resource.Layout.filaArticulo, parent, false);

        // Create a ViewHolder to find and hold these view references, and 
        ArticuloViewHolder vh = new ArticuloViewHolder(itemView,OnClick);
        vh.Arrow.Click += (o, e) =>
        {
            if (vh.Info.Visibility == ViewStates.Gone)
                vh.Info.Visibility = ViewStates.Visible;
            else
                vh.Info.Visibility = ViewStates.Gone;
        };
        return vh;
    }

    // Fill in the contents
    public override void OnBindViewHolder(ViewHolder holder, int position)
    {
        ArticuloViewHolder vh = holder as ArticuloViewHolder;
        if (listado[position].imagen != null)
            vh.Image.SetImageBitmap(listado[position].BitImage);
        vh.Caption.Text = listado[position].nombre;
        vh.Cantidad.Text = "DISPONIBLE :" + listado[position].cantidad;


    }
    void OnClick(int position)
    {
        if (ItemClick != null)
            ItemClick(this, position);
    }

    // Return the number of photos available in the photo album:
    public override int ItemCount
    {
        get { return listado.Count; }
    }

    public class ArticuloViewHolder : ViewHolder
    {
        public ImageView Image { get; private set; }
        public TextView Caption { get; private set; }
        public TextView Cantidad { get; private set; }
        public LinearLayout Info { get; private set; }
        public ImageButton Arrow { get; private set; }

        // Get references to the views defined in the CardView layout.
        public ArticuloViewHolder(View itemView, Action<int> listener)
            : base(itemView)
        {
            Image = itemView.FindViewById<ImageView>(Resource.Id.foto);
            Caption = itemView.FindViewById<TextView>(Resource.Id.nombre);
            Cantidad = itemView.FindViewById<TextView>(Resource.Id.cantidad);
            Arrow = itemView.FindViewById<ImageButton>(Resource.Id.arrowButt);
            Info = itemView.FindViewById<LinearLayout>(Resource.Id.infoLay);
            itemView.Click += (sender, e) => listener(base.Position);
        }
    }
}

这是行的代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:card_view="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">
        <ImageView
            android:layout_width="150px"
            android:layout_height="150px"
            android:id="@+id/foto"
            android:scaleType="centerCrop"
            android:layout_gravity="center"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#333333"
            android:text="Caption"
            android:id="@+id/nombre"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="4dp" />
    <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:id="@+id/infoLay"
          android:visibility="gone"
          android:padding="8dp">
         <TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceMedium"
              android:textColor="#333333"
              android:text="Caption"
              android:id="@+id/cantidad"
              android:layout_gravity="center_horizontal"
              android:layout_marginLeft="4dp" />
      </LinearLayout>

      <ImageButton
            android:id="@+id/arrowButt"
            android:layout_width="match_parent"
            android:src="@drawable/down"
            android:layout_height="50dp"/>
      </LinearLayout>
</android.support.v7.widget.CardView>   

1 个答案:

答案 0 :(得分:1)

其他显示的项目是使用相同(回收)视图持有者和视图的项目。 您需要(重新)设置InfoOnBindViewHolder()部分的可见性。

在您的情况下,由于您不介意在向后滚动时是否再次隐藏该项目,您可以执行以下操作:

public override void OnBindViewHolder(ViewHolder holder, int position)
{
    ...

    vh.Info.Visibility = ViewStates.Gone
}