如果展开,如何在第二次单击时关闭展开的列表项?

How to close an expanded list item on second click if it's expanded?

我有回收站观点。我在列表项视图中添加了一个可扩展视图。单击它会展开并显示布局。但是现在我想在打开时单击相同的项目将其关闭。

现在,如果我单击第一个项目,布局会展开,然后如果我单击第二个项目,第二个项目的布局会展开,第一个项目的布局会关闭。

我已经关注了这个 link :

RecyclerView expand/collapse items

适配器:

 public class BookingsAdapter extends RecyclerView.Adapter<BookingsAdapter.MyViewHolder> implements View.OnClickListener{

    private int expandedPosition = -1;
    private List<Bookings> bookingsList;
    int status;
    Context context;

    public interface OnItemClickListener {
        void onItemClick(Events item);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView eventName,eventType,eventDateTime,userName;
        public RelativeLayout expandLayout;
        public CardView cardView;


        public MyViewHolder(View view) {
            super(view);
            eventName = (TextView) view.findViewById(R.id.text_eventName);
            eventType = (TextView) view.findViewById(R.id.textView_EventType);
            eventDateTime = (TextView) view.findViewById(R.id.text_dateTime);
            userName = (TextView) view.findViewById(R.id.text_userName);
            expandLayout = (RelativeLayout) view.findViewById(R.id.expandLayout);
            cardView = (CardView) view.findViewById(R.id.card_view);

        }
    }


    public BookingsAdapter(ArrayList<Bookings> bookingsList,Context context) {
        this.bookingsList = bookingsList;
        this.context = context;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.bookings_card, parent, false);

        MyViewHolder holder = new MyViewHolder(itemView);

        // Sets the click adapter for the entire cell
        // to the one in this class.
        holder.itemView.setOnClickListener(BookingsAdapter.this);
        holder.itemView.setTag(holder);

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Bookings bookings = bookingsList.get(position);
        holder.eventName.setText(bookings.getEventName());
        holder.eventType.setText(bookings.getEventType());
        holder.eventDateTime.setText(bookings.getEventDateTime());
        holder.userName.setText(bookings.getUserName());

        if (position == expandedPosition) {

            holder.expandLayout.setVisibility(View.VISIBLE);

        } else {

            holder.expandLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View view) {
        MyViewHolder holder = (MyViewHolder) view.getTag();

        Bookings bookingsItem = bookingsList.get(holder.getPosition());

        // Check for an expanded view, collapse if you find one


            if (expandedPosition >= 0) {
                int prev = expandedPosition;
                notifyItemChanged(prev);
            }
            // Set the current position to "expanded"
            expandedPosition = holder.getPosition();
            notifyItemChanged(expandedPosition);



        Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
    }
    @Override
    public int getItemCount() {
        return bookingsList.size();
    }
}

布局xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@android:color/white"
        android:layout_marginTop="05dp"
        android:layout_marginRight="05dp"
        android:layout_marginLeft="05dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/parentLayout">


       <RelativeLayout
           android:layout_width="wrap_content"
           android:layout_height="90dp"
           android:id="@+id/detailsLayout">

        <RelativeLayout
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="05dp"
            android:layout_marginBottom="05dp"
            android:id="@+id/eventLayout">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Event name"
                android:id="@+id/text_eventName"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textSize="14sp"
                android:textColor="@android:color/black" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="("
                android:id="@+id/textView26"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignTop="@+id/textView_EventType"
                android:textSize="12sp"
                android:textColor="@color/colorAccent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date and time"
                android:id="@+id/text_dateTime"
                android:layout_below="@+id/textView26"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="05dp"
                android:textSize="12sp"
                android:textColor="@color/colorAccent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Event type"
                android:id="@+id/textView_EventType"
                android:layout_below="@+id/text_eventName"
                android:layout_toRightOf="@+id/textView26"
                android:layout_toEndOf="@+id/textView26"
                android:layout_marginTop="05dp"
                android:textSize="12sp"
                android:textColor="@color/colorAccent" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=")"
                android:id="@+id/textView29"
                android:layout_alignTop="@+id/textView_EventType"
                android:layout_toRightOf="@+id/textView_EventType"
                android:layout_toEndOf="@+id/textView_EventType"
                android:textSize="12sp"
                android:textColor="@color/colorAccent" />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/place_autocomplete_separator"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="false"
                android:layout_alignParentTop="true">
            </View>
        </RelativeLayout>

        <View
            android:layout_width="3dp"
            android:layout_height="wrap_content"
            android:background="@color/grey">
        </View>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/eventLayout"
            android:layout_toRightOf="@+id/eventLayout"
            android:layout_toEndOf="@+id/eventLayout"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignBottom="@+id/eventLayout"
            android:id="@+id/profilepicLayout">


            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@drawable/ic_person_black_48dp"
                android:id="@+id/profileImage"
                android:layout_centerHorizontal="true"
                android:layout_alignParentTop="false"
                android:background="@drawable/circle" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User Name"
                android:id="@+id/text_userName"
                android:layout_below="@+id/profileImage"
                android:layout_marginTop="05dp"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:textAlignment="center" />
        </RelativeLayout>


       </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="false"
                android:layout_alignParentStart="false"
                android:layout_below="@+id/detailsLayout"
                android:id="@+id/expandLayout"
                android:visibility="gone">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/place_autocomplete_separator"></View>

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView12"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:background="@drawable/ic_call_black_18dp"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="05dp"
                    android:layout_marginBottom="05dp" />

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView13"
                    android:layout_alignTop="@+id/imageView12"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/ic_textsms_black_18dp" />

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:id="@+id/imageView14"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="30dp"
                    android:layout_marginEnd="30dp"
                    android:background="@drawable/ic_chat_black_18dp"
                    android:layout_centerVertical="true"
                    android:layout_alignTop="@+id/imageView13" />
            </RelativeLayout>
       </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

编辑:我在 class 中添加了布尔变量并执行了此操作。注意发生。当我点击 item

时布局没有关闭
  @Override
public void onClick(View view) {
    MyViewHolder holder = (MyViewHolder) view.getTag();

    Bookings bookingsItem = bookingsList.get(holder.getPosition());

    // Check for an expanded view, collapse if you find one
    // set previously expanded row to false
    for(int i=0;i<bookingsList.size();i++)
    {
        if(bookingsList.get(i).expanded)
        {
            bookingsList.get(i).expanded = false;
        }
    }
    //set current item expanded
    bookingsList.get(holder.getPosition()).expanded = true;

       if (expandedPosition >= 0) {
            int prev = expandedPosition;
            notifyItemChanged(prev);
        }
        // Set the current position to "expanded"
        expandedPosition = holder.getPosition();
        notifyItemChanged(expandedPosition);



    Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}

现在我想在单击项目时关闭同一项目的展开布局。

注意:在 class Bookings 中取一个名为 expanded 的布尔变量,默认情况下将其保存为 false 在您将值添加到列表的位置,然后在点击时执行类似这样的操作

 public class BookingsAdapter extends RecyclerView.Adapter<BookingsAdapter.MyViewHolder> implements View.OnClickListener{

private List<Bookings> bookingsList;
int status;
Context context;

public interface OnItemClickListener {
    void onItemClick(Events item);
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView eventName,eventType,eventDateTime,userName;
    public RelativeLayout expandLayout;
    public CardView cardView;


    public MyViewHolder(View view) {
        super(view);
        eventName = (TextView) view.findViewById(R.id.text_eventName);
        eventType = (TextView) view.findViewById(R.id.textView_EventType);
        eventDateTime = (TextView) view.findViewById(R.id.text_dateTime);
        userName = (TextView) view.findViewById(R.id.text_userName);
        expandLayout = (RelativeLayout) view.findViewById(R.id.expandLayout);
        cardView = (CardView) view.findViewById(R.id.card_view);

    }
}


public BookingsAdapter(ArrayList<Bookings> bookingsList,Context context) {
    this.bookingsList = bookingsList;
    this.context = context;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.bookings_card, parent, false);

    MyViewHolder holder = new MyViewHolder(itemView);

    // Sets the click adapter for the entire cell
    // to the one in this class.
    holder.itemView.setOnClickListener(BookingsAdapter.this);
    holder.itemView.setTag(holder);

    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Bookings bookings = bookingsList.get(position);
    holder.eventName.setText(bookings.getEventName());
    holder.eventType.setText(bookings.getEventType());
    holder.eventDateTime.setText(bookings.getEventDateTime());
    holder.userName.setText(bookings.getUserName());

    if (bookings.expanded) {

        holder.expandLayout.setVisibility(View.VISIBLE);

    } else {

        holder.expandLayout.setVisibility(View.GONE);
    }

}

 @Override
public void onClick(View view) {
    MyViewHolder holder = (MyViewHolder) view.getTag();

   if(bookingsList.get(holder.getPosition()).expandad)
    {
    bookingsList.get(holder.getPosition()).expandad=false;
    notifyDataSetChanged();
    }
    else{
    // set previously expanded row to false
    for(int i=0;i<bookingsList.size();i++)
      {
         if(bookingsList.get(i).expanded)
           {
            bookingsList.get(i).expandad=false;
           }
         }
    //set current item expanded 
    bookingsList.get(holder.getPosition()).expandad=true;  
    notifyDataSetChanged();
    }
    Toast.makeText(context, "Clicked: "+ bookingsItem.getEventName(), Toast.LENGTH_SHORT).show();
}