0

I have a recycler view. I have added an expandable view in the list item view. On a click it expands and show the layout. But now I want to close it on click of same item if its open.

Now if I click on 1st item the layout gets expanded and then if i click on 2nd item layout for 2nd item gets expanded and layout for 1st item gets closed.

I have followed this link :

RecyclerView expand/collapse items

Adapter:

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

Layout 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>

EDIT: I have added boolean variable in class and did this. Noting happens. Layout dose not get close when I click on 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();
}

Now I want to close the expanded layout of same item on click of item.

Community
  • 1
  • 1
Sid
  • 2,792
  • 9
  • 55
  • 111

1 Answers1

2

NOTE: Take a boolean variable named expanded in class Bookings and by default save it as false where you are adding values to your list then in your on click do something like this

 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();
}
Digvijay Singh
  • 623
  • 9
  • 26