Search code examples
androidvideoandroid-recyclerviewandroid-nestedscrollviewautostart

Autostarting of Video when Recyclerview is inside NestedScrollview


I have a recyclerview inside a NestedScrollview. The recyclerview loads both images and videos. There is no problem with the image. But I have applied some library called Toroplayer for the videos which auto starts when it gets the focus. Now, while using only recyclerview, the videos loaded inside it were getting the focus and hence were starting like expected. But when I use NestedScrollview over Recyclerview, the videos do not get the focus, hence do not autostart (this is what I think the reason of the issue is. I maybe wrong. But when I put the videoview outside the Nestedscrollview the videos autostart properly.) I need to use Nestedscrollview, cannot ignore it.

Here is the part of the XML:

<android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/swipe_refresh_layout"

        android:layout_marginTop="8dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


        <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fillViewport="true"
        android:overScrollMode="never"
            android:id="@+id/hometimeline"
            app:layout_behavior="com.evs.demo.layout.FixedScrollingViewBehavior">



                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >


                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="80dp"
                        android:layout_marginTop="1dp"
                        android:background="#fff"
                        android:id="@+id/statusView"
                        android:orientation="horizontal"
                        android:layout_marginStart="4dp"
                        android:layout_marginEnd="4dp"
                        android:layout_marginBottom="2dp"
                        />

                    <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:cardElevation="18dp"

                        >

                        <im.ene.toro.widget.Container
                            xmlns:android="http://schemas.android.com/apk/res/android"
                            android:id="@+id/home_blog_list"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginStart="14dp"
                            android:layout_marginEnd="14dp"
                            android:nestedScrollingEnabled="false"
                            app:layoutManager="LinearLayoutManager"
                            app:layout_behavior="@string/appbar_scrolling_view_behavior"
                            />

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




                </LinearLayout>



    </android.support.v4.widget.NestedScrollView>


        </LinearLayout>


    </android.support.v4.widget.SwipeRefreshLayout>

Since the toroplayer (the library of videoplayer that I am using) starts on focus, I need to have a way where the childred in recyclerview gets the focus over the nestedscrollview.


Solution

  • What's the alternative?

    The "natural" alternative to this issue where you don't want to rely on a NestedScrollView is to use the ViewType parameter of a RecyclerView and inflate individual views depending on your content. So the entire thing scrolls thanks to a RecyclerView and it all works. But this is a little bit more work, for you have to create view types for all the different content and prepare the list so the RecyclerView receives a nice formatted list of a common item that can describe what you need to display. It's more work but once you got it working, it's very reliable.

    Looking at your Layout, you have the following hierarchy:

    <SwipeRefresh>
      <LinearLayout>
         <NestedScrollView>
            <LinearLayout>
                <RecyclerView />
                <CardView>
                   <toro.widget.Container />
                </CardView>
            </LinearLayout>
          </NestedScrollView>
       </LinearLayout>
    </SwipeRefresh>
    

    If I didn't need to use the NestedScrollView, I'd flatten it out with Constraint Layout. The way I understand your layout, you have your recyclerview with a fixed 80dp height, and below you fit this carview which has a container for something (the video player?), fixed at the bottom.

    You mention you need to use NestedScrollView, and I don't know why, but if you for some reason realize you don't need it, this is how I'd implement the layouts:

    <SwipeRefresh>
       <ConstraintLayout>
          <RecyclerView />
          <CardView>
             <Toro Container>
          </CardView>
        </ConstraintLayout>
    </SwipeRefresh>
    

    In this, the swipe will encompass the ConstraintLayout which will use the entire screen provided by/to the Swipe layout, and the RecyclerView will be at a 80dp height at the top, next will be the CardView, using all the remaining space, and inside the Toro Container will also use as much space as the CardView is given.

    If you perhaps clarify why you'd need a NestedScrollView, then perhaps we can rethink this, but short of this, if you trully need the Nesting, then you will need to play (and possibly extend NestedScrollView) to see what the focus is doing when you tap those children. Debug where the focus is going, and take it from there. :/