0

I am using recyclerview for showing data from web service. Based on particular condition i need to remove the particular recycler view item. So i am using View.Gone. But it is showing empty space. I googled about this issue and set the height to wrapcontent, but it is not working. Please any one help me.

my code: xml: item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/businfo"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        app:contentPaddingBottom="10dp"
        app:contentPaddingTop="16dp"
        android:elevation="10dp"
        android:layout_margin="5dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/travel_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="14sp"
            android:layout_marginLeft="5dp"
            android:textColor="@color/holo_blue_light"
            android:text="CGR Travels"/>


        <TextView
            android:id="@+id/bus_price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:textSize="14sp"
            android:textColor="#F04C3B"
            android:layout_marginRight="30dp"
            android:text="Rs.349"/>


    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="5dp"
        android:background="#b6b6b6"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/arrtime_desttime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:textSize="15sp"
            android:textColor="#1e356a"
            android:text="9.00P.M - 7.00AM"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:textSize="12sp"

            android:layout_marginRight="30dp"
            android:text="38 seats"
            android:id="@+id/seatcount" />




    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:layout_marginTop="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="12sp"
            android:layout_marginLeft="5dp"
            android:text="2+1 semi sleeeper"
            android:id="@+id/bustype" />
        <TextView
            android:id="@+id/cancellationpolicy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:textSize="12sp"
            android:textColor="@color/holo_blue_light"
            android:layout_marginRight="30dp"
            android:text="Cancellation policy"
           />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:id="@+id/routeid"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:id="@+id/dropdate"/>


    </LinearLayout>

</LinearLayout>

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

java:

@Override
        public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
if ((bus_routes.get(position).getAvailableSeats() == 0)) {
                viewHolder.businfo.setVisibility(View.GONE);


            } else {
                viewHolder.businfo.setVisibility(View.VISIBLE);

            }
}
kartheeki j
  • 2,206
  • 5
  • 27
  • 51

5 Answers5

12

Although removing the item from list and then updating list (notifydatasetchanged()) is a good practice but in some cases like when you have multiple viewHolders in a signle recyclerView it may be complicated, so in order to hide an specific item in your recyclerView you should do as below:
- Dont hide the root view of your layout, instead add a child to rootView of your layout which contains all other views and hide that. for example in your case you can set your cardView visibity to View.GONE and dont make any change to its parent linearLayout (businfo). i had similar case and this worked for me.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!--  don't change visibility of rootView view -->

    <LinearLayout
        android:id="@+id/child_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--  Place all child views inside here -->
        <!--  change visibility of child_container view -->

    </LinearLayout>

</LinearLayout>
Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
  • Looks like it's android bug, your solution worked for me too. would you explain more details about it or share related links if any? – Mehdi Dehghani Apr 23 '19 at 12:28
  • while this is correct, if you are using a `RecyclerView.ItemDecoration` you will still get dividers showing up. – Raphael C Sep 04 '19 at 13:05
  • This works for situations where you can't remove elements from the collection. In my case I was building an expanding recycler with a single backing list, and needed elements to show and hide without modifying the list. – Brill Pappin Jun 04 '20 at 14:55
0

You should remove this item from your bus_routes:

bus_routes.remove(position);

And then:

notifyItemRemoved(position);
notifyItemRangeChanged(position, bus_routes.size());
shmakova
  • 6,076
  • 3
  • 28
  • 44
0

You can not directly hide view this way. you have to remove unwanted items from your ArrayList or don't add unwanted item before setting adapter. If your items are updated then again remove unwanted items and call notifydataset changed

Rajesh N
  • 6,198
  • 2
  • 47
  • 58
0

Remove this element from ArrayList and use notifyDataSetChanged();

Example :

holder.imageDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            paths.remove(position);
            notifyDataSetChanged();
        }
    });
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

You dont need to set visible or gone just remove item from list and than update list.

@Override
    public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
    if ((bus_routes.get(position).getAvailableSeats() == 0)) {
            bus_routes.remove(position);

        }
    }

try to call youradapter.notifydatasetchanged() after set adapter to recyclerview from activity.

Upendra Shah
  • 2,218
  • 17
  • 27