0

My RecyclerView contains a list of CardView

xml for MainActivity:

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
            <android.support.v7.widget.Toolbar/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/view_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.NestedScrollView>

    <FloatingActionButton/>

</android.support.design.widget.CoordinatorLayout>

I use an adapter for the RecyclerView above to contain the Cards.

xml used to inflate ViewHolder inside the adapter:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout"
    android:paddingTop="2dp"
    android:paddingRight="2dp"
    android:paddingLeft="2dp"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">


    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="@android:color/holo_red_light"
        card_view:cardPreventCornerOverlap="true"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="3dp"
        card_view:contentPadding="7dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:id="@+id/relat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:focusable="true"  
            android:focusableInTouchMode="false"
            android:padding="10dp">

            <TextView/>
            //...

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

</RelativeLayout>

To make the Cards clickable, I tried all solutions in these - two popular posts , but I always have this weird bug:

The list of Cards won't scroll when I start the app for the first time, unless I click on the RecyclerView once. It's as if the RecyclerView is not in focus initially.

Also, if I get rid of all click listeners or similar ways to make the CardView's clickable, and only keep the focusable code in xml:

           android:focusable="true"  
           android:focusableInTouchMode="false"

, then it does scroll right away, but as soon as I add any click (listener) mechanism, or even include "android:clickable="true"" for the ViewHolder, that bug re-emerges. Please advise. Thank you

Community
  • 1
  • 1
DanSoren
  • 173
  • 2
  • 9

2 Answers2

1

You should never nest a RecyclerView inside a ScrollView. Just remove the NestedScrollView and the RecyclerView should take care of its scrolling behavoiur.

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
            <android.support.v7.widget.Toolbar/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/view_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • changing this alone doesn't solve the problem for me. After removing the NestedScrollView the RecyclerView area is not scroll-able anymore. – DanSoren Sep 07 '16 at 03:33
  • @DanSoren What is the version of the support libraries you're using? – Ahmed Hegazy Sep 07 '16 at 03:36
  • Also Can you try using `android:layout_height="match_parent"`? – Ahmed Hegazy Sep 07 '16 at 03:37
  • com.android.support:appcompat-v7:24.1.1; I tried match_parent and still the RV doesn't scroll. – DanSoren Sep 07 '16 at 04:09
  • I now start to suspect that the implementation I've used to make CardView clickable might be the reason for this. Afterall, that decides how the RV & CardView reacts to touch/click actions. But also it could be something as trivial as focusable/clickable property setting..debugging this is so frustrating – DanSoren Sep 08 '16 at 00:21
0

Turns out the scrolling issue is not related to the RecyclerView. It was due to an open source widget I used which anchored to the RV and somehow interfered with the focusing/scrolling/touch interception. Finally got rid of this bug after days of looking elsewhere..

thank you all the same

DanSoren
  • 173
  • 2
  • 9