1

I have a recyclerview in my app showing a list of content. If a user is subscribed, they can proceed to read the full content and if not the recyclerview is hidden and a subscribe layout is shown.

Layout file

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   android:background="@color/light_grey"
   android:orientation="vertical"
   android:weightSum="10"
   tools:layout_editor_absoluteY="25dp">


<!--RECYCLER VIEW-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="5dp"
    android:layout_weight="8.3"
    android:padding="2dp"
    android:visibility="gone" />

  <!--SUBSCRIBE TO VIEW CONTENT LAYOUT-->
  <RelativeLayout
    android:id="@+id/rlSubscribeToView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:gravity="center"
    android:visibility="gone"
    android:layout_margin="10dp"
    android:elevation="48dp"
    android:layout_gravity="center"
    android:layout_weight="8.3">

    <TextView
        android:id="@+id/tvSubscribe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textColor="@color/maroon"
        android:text="@string/subscribe_to_view"/>

    <Button
        android:id="@+id/btnSubscribe"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="15dp"
        android:layout_marginStart="20dp"
        android:layout_below="@id/tvSubscribe"
        android:layout_marginEnd="20dp"
        android:text="@string/subscribe"
        android:textColor="@color/white" />

  </RelativeLayout>

and the

RecyclerView Adapter

    public void onBindViewHolder(@NonNull final ReadViewholder, final int  position) {

    holder.readMoreButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (userIsSubscribed) {
                //Launch the next Activity
            } else {
               //Show the subscribe layout
                holder.rlSubscribeToView .setVisibility(View.VISIBLE);

               //Then hide the entire recyvlerView
            }

        }
    });
}

class ReadViewholder extends RecyclerView.ViewHolder {

RelativeLayout rlSubscribeToView;
Button readMoreButton;

ReadViewholder(@NonNull View itemView) {
    super(itemView);

    //Not able to find (rlSubscribeToView ) since its not inside the recyclerview
    rlSubscribeToView = itemView.findViewById(R.id.rlSubscribeToView);
    readMoreButton= itemView.findViewById(R.id.readMoreButton);
  }
}

How can I get access to the subscribe layout (rlSubscribeToView), which is on the same layout file as the recycler view and also hide the entire recycler view in the Adapter?

Boron
  • 99
  • 10
  • 34
  • You're missing once thing here: your subscribe view is not part of your `RecyclerView` adapter so you can't reference it from your adapter. – Jeel Vankhede Jan 18 '19 at 10:08
  • @Ibra Would you be more specific? because if isUserSubscribed is per item level, then you meants to hide that row and show subscribe layout for that specific row or if it's on global level like either show full list or show subscribe layout then you should get that isUserSubscribed value from other source than list. in this case, you can write logic of hide/show in your fragment or activity – bhumik Jan 18 '19 at 10:13
  • @JeelVankhede yes, its not part of the `RecyclerView` but I need to get a reference to it so that I can hide the RecyclerView and display it – Boron Jan 18 '19 at 10:18
  • Suggestion : don't reference it in adapter, rather use **callback** to your activity from adapter conditionally & do your **show/hide** stuff in activity. – Jeel Vankhede Jan 18 '19 at 10:33
  • Either use an interface if you are comfortable tightly coupling the adapter and the activity or a Local Broadcast for loose coupling. – Srikar Reddy Jan 18 '19 at 10:50

2 Answers2

2

From a little bit of research, the are 2 callbacks that get can get you a reference to the actual RecyclerView, the onAttachedToRecyclerView and the onDetachedFromRecycler methods. My guess is that you are calling the Adapter constructor and passing in a context. If so use the below code, it will produce your desired result.

RelativeLayout rlSubscribeToView;
RecyclerView recyclerView;

public RecyclerAdapter(Context context) {
    this.context = context;
    this.videoItems = videoItems;

    rlSubscribeToView = ((Activity) context).findViewById(R.id.rlSubscribeToView);
}

 @Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    this.recyclerView = recyclerView;
}

and in your onBindViewHolder you can now get access the subscribe layout

public void onBindViewHolder(@NonNull final ReadViewholder, final int  position) {
 ...
   rlSubscribeToView.setVisibility();
}
Oush
  • 3,090
  • 23
  • 22
1

What you need is an interface passed in as a parameter when you create your Adapter.

Example:

class Adapter(private val actions: Actions) : RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // Create ViewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Setup Binder

        holder.readMoreButton.setOnClickListener(View.OnClickListener {
             if (userIsSubscribed) {
                 actions.launchActivity() //Launch the next Activity
             } else {
                 //Show the subscribe layout
                 holder.rlSubscribeToView.setVisibility(View.VISIBLE)

                 actions.hideRecylerView() //Then hide the entire recyclerView
             }
         })
     }
}

internal interface Actions {
    fun launchActivity()
    fun hideRecylerView()
}
Bam
  • 478
  • 6
  • 19