I have a non-scrolling RecyclerView, via setting layout_height="wrap_content".
It is embedded within a NestedScrollView, so the main screen scrolls but the RecyclerView itself does not. I set it this way since I only have 3 items in the RV, but I still want the dynamic setup that comes with the RV.
The Adapter for the RV creates a new ExoPlayer for each item in the RV.
Questions
How can I stop and release all the players when leaving/replacing this Fragment?
How can I stop or pause any player which scrolls out of view?
I tried catching the RV scroll viaonViewDetachedFromWindow()from the Adapter, but it's not being called. Maybe since the RV itself is not scrolling, rather the NestedScrollView is the one scrolling.How do I access the players of each item from outside?
For example -and this sounds a bit lame- , if I wanted to loop through all items in the RV and manually stop their players.
Layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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_height="wrap_content"
android:layout_width="match_parent"
tools:context=".ui.main.wall_screen.WallFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/j_background"
android:orientation="vertical"
>
.... a bunch of stuff here....
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="18dp"
android:layout_marginBottom="5dp"
android:layout_weight="0.8"
android:fontFamily="sans-serif"
android:textStyle="normal"
android:maxLines="1"
android:text="@string/DESCUBRE"
android:textColor="@color/bottom_nevi_blue"
android:textSize="14sp"
app:autoSizeTextType="uniform"
tools:ignore="NestedWeights" />
..... Here is the RecyclerView in question .....
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/WF_news_feed_RV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="@+id/WF_see_more_news_Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="15dp"
android:drawableTop="@drawable/eye_icon"
android:textColor="@color/bottom_nevi_blue"
android:background="@android:color/transparent"
android:text="@string/DESCUBRE_MÁS"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
For the fist and last question I would think that the ExoPlayer would close as soon as the fragment goes through onPause(). If not I think this is what you are looking for
yourRecyclerView.findViewHolderForAdapterPosition(adapterPosition);Since you only have three items in that RecyclerView you can easily loop through using 0 to 2 as the adapter position. You would then be able to stop all the players when you navigate away from the fragment or get a specific ViewHolder if you wanted.
For the second part of your question I think you would still use the method from above. Just declare a member variable for the root view of the ViewHolder and you can then get the height of that specific ViewHolder that way. Then using a Scroll Listener on your ScrollView you can calculate if that view has scrolled off screen.
Only caveat's I can think of when getting the height of the ViewHolders root view is that it probably won't account for margins, secondly you will need to also get the height of the LinearLayout above the RecyclerView.