-3

I am using a recycler view in one of my activities and I am populating that recycler view using an ArrayList. However, I wish to hide an element (which is in the layout file example_item.xml). Is there a way I can do this?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.circularreveal.CircularRevealRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:numberpicker="http://schemas.android.com/apk/res-auto"
    tools:context=".FlightResults">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:padding="4dp"
        android:scrollbars="vertical">

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

</android.support.design.circularreveal.CircularRevealRelativeLayout>

Xml layout of the activity I'm working in.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginBottom="4dp"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <ImageView
            android:id="@+id/outbound"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp"
            android:src="@drawable/ic_return"/>

        <TextView
            android:id="@+id/fromTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/outbound"
            android:text="From - To"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fromTo"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/outbound"
            android:text="Time leaving - time arriving"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/returning"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/outbound"
            android:padding="2dp"
            android:src="@drawable/ic_outbound"/>

        <TextView
            android:id="@+id/fromTo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_toEndOf="@+id/returning"
            android:layout_below="@id/time"
            android:text="From - To"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/time2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fromTo2"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@+id/returning"
            android:text="Time leaving - time arriving"
            android:textSize="15sp" />

    </RelativeLayout>


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

Layout i wish to change (an element from this layout - id:returning)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Akshat S
  • 1
  • 3
  • it is possible as long as the view exists but i don't understand where is the view that you want to hide and from where and when do you want to hide it? – Master Fathi Apr 13 '19 at 22:32
  • 1
    Where are you trying to hide that view from (the activity that has the `RecyclerView`)? What view specifically are you trying to hide? What does your activity and adapter classes look like? There's a lot of missing information here. We can't help unless we have the entire picture. – michael Apr 13 '19 at 22:46

1 Answers1

0

if the view is in the sameActivity as your adapter you should just pass the view to the constructor of your adapter and when you want to hide just hide it.

if the view is in another activity you just need to create a callback and when that callback is called you can do what ever you want in that other activity.

In your activity where you want the event for changing the view to be called add this code.

private static OnTagItemClickListener onTagClickListener;

public static void setOnTagClickListener(OnTagItemClickListener onTagClick) {
        onTagClickListener = onTagClick;
    }


    public static interface OnTagItemClickListener {
        void onTagItemClickListener(Object params //in here you can pass what ever you want);
    }

and from Activity1 when ever you want to call this callback you just do

onTagClickListener.onTagItemClickListener(object); // pass that object to the callback

now from your activity to change the visibility of a view add this

Activity1.setOnTagClickListener(new Activity1.OnTagItemClickListener() {
            @Override
            public void onTagItemClickListener(Object params) {
                yourView.setVisibility(View.GONE);
            }
        });

this is more explained than my example you should go take a look. How to Define Callbacks in Android?

Master Fathi
  • 349
  • 1
  • 9
  • 19