0

From some time i was facing issue with scrollbars when recyclerview/Nestedscrollview put inside NestedSCrollView. I know however it is not good practice to have scroll layout inside another scroll layout, but have to fix this issue in one of my app.

i.e. when you try to scroll list by dragging vertical scrollbar it scrolling breaks after few pixels. Tried every but did not successes.

Here is testing layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        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"
        tools:context=".TestingScrollbars">

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:expanded="false"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:gravity="bottom"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"/>

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


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

    <android.support.constraint.ConstraintLayout
            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"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".TestingScrollbars"
            tools:showIn="@layout/activity_main">

        <android.support.v4.widget.NestedScrollView
                android:layout_gravity="bottom|end"
                android:layout_margin="@dimen/fab_margin"
                android:layout_width="match_parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintVertical_bias="0"
                android:layout_height="wrap_content">
            <RelativeLayout android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                <TextView android:id="@+id/tv"
                          android:layout_width="match_parent"
                          android:layout_height="400dp"
                          android:gravity="center"
                          android:text="testing"/>
                <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerview"
                        android:layout_below="@+id/tv"
                        android:scrollbars="vertical"
                        android:fadeScrollbars="false"
                        android:nestedScrollingEnabled="true"
                        android:layout_width="wrap_content"
                        android:padding="15dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintStart_toEndOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintVertical_bias="0"
                        android:layout_height="wrap_content">
                </android.support.v7.widget.RecyclerView>

            </RelativeLayout>

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

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

Activity.kt

package com.example.testingscrollbars

import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class TestingScrollbars : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = CustomAdapter(getAdapterList())
        recyclerView.isVerticalScrollBarEnabled = true
    }

    private fun getAdapterList(): List<Item> {
        val list = ArrayList<Item>()
        list.add(Item("first pri. text", "first sec. text"))
        list.add(Item("second pri. text", "second sec. text"))
        list.add(Item("third pri. text", "third sec. text"))
        list.add(Item("something1 pri. text", "something1 sec. text"))
        list.add(Item("something2 pri. text", "something2 sec. text"))
        list.add(Item("something3 pri. text", "something3 sec. text"))
        list.add(Item("something4 pri. text", "something4 sec. text"))
        list.add(Item("something5 pri. text", "something5 sec. text"))
        list.add(Item("something6 pri. text", "something6 sec. text"))
        list.add(Item("something7 pri. text", "something7 sec. text"))
        list.add(Item("something8 pri. text", "something8 sec. text"))
        list.add(Item("something9 pri. text", "something9 sec. text"))
        list.add(Item("something10 pri. text", "something10 sec. text"))
        list.add(Item("something11 pri. text", "something11 sec. text"))
        list.add(Item("something12 pri. text", "something12 sec. text"))
        list.add(Item("something13 pri. text", "something13 sec. text"))
        list.add(Item("something14 pri. text", "something14 sec. text"))
        list.add(Item("something15 pri. text", "something15 sec. text"))
        return list
    }

    class Item(val primary: String, val secondary: String)

    class CustomAdapter(val list: List<Item>) : RecyclerView.Adapter<ViewHolder>() {


        override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
            p0.tvPrimary.text = list[p1].primary
            p0.tvSecondary.text = list[p1].secondary
        }

        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder =
            ViewHolder(LayoutInflater.from(p0.context).inflate(R.layout.recycler_item, p0, false))

        override fun getItemCount(): Int = list.size

    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvPrimary = view.findViewById<TextView>(R.id.tv_primary)
        val tvSecondary = view.findViewById<TextView>(R.id.tv_Secondary)
    }
}

Electro
  • 395
  • 4
  • 16

1 Answers1

0

There is already a question about it. It might help you. RecyclerView inside ScrollView is not working

But the best practice is not to use a scrolling component inside another scrolling component. This will confuse the OS and it won't be able to decide which component to scroll when you start dragging.

Sahil
  • 86
  • 2
  • 6
  • yes, however this question is little different above question is well handled using nested scrollview android making setNestedScrollView = true, however everything is working well but while dragging scrollbar, scroll breaks after few pixels. I know approach is wrong i think lot of time we need to put nested scrolling.... :( – Electro Jul 24 '19 at 06:02