Nitzan Daloomy
Nitzan Daloomy

Reputation: 316

ScrollView doesn't scroll in a ViewPager

In my app I use a TabLayout under a Toolbar, and a ViewPager filling the rest of the space. I need one of the fragments under the tab to be able to scroll. tried using ScrollView, NestedScrollView and android:fillViewport="true" with no success.

I've looked at other questions about the same problem, tried anything that was suggested in the answers, but they all didn't solve my problem.

the activity that contains the ViewPager's code:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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"
    android:orientation="vertical"
    tools:context=".UserProfile">

    <include
        android:id="@+id/userToolbar"
        layout="@layout/user_info_toolbar_layout" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/userToolbar"
        android:background="@android:color/holo_blue_light"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="@android:color/black">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/dashboardTab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dashboard_tab_text_en" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/statisticsTab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/statistics_tab_text_en" />

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/userInfoViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tabLayout" />


</androidx.constraintlayout.widget.ConstraintLayout>

the fragment's code:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    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:id="@+id/user_dashboard_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    tools:context=".Dashboard">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:padding="40dp"
        android:id="@+id/userInfoContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.github.abdularis.civ.CircleImageView
            android:id="@+id/userImage"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:padding="20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/male_user_sample1"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/username_sample"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="30sp"
            app:layout_constraintTop_toBottomOf="@id/userImage" />

        <com.ramijemli.percentagechartview.PercentageChartView
            android:id="@+id/userLevelProgress"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="20dp"
            android:outlineAmbientShadowColor="@color/colorPrimary"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/username"
            app:pcv_animDuration="1000"
            app:pcv_animInterpolator="anticipate_overshoot"
            app:pcv_drawBackground="false"
            app:pcv_drawBackgroundBar="false"
            app:pcv_mode="ring"
            app:pcv_orientation="counter_clockwise"
            app:pcv_progress="100"
            app:pcv_startAngle="270"
            app:pcv_textColor="@color/colorPrimary"
            app:pcv_textShadowRadius="100"
            app:pcv_textSize="50sp" />

        <TextView
            android:id="@+id/userLevel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/level_sample"
            android:textColor="@color/colorPrimary"
            android:textSize="40sp"
            app:layout_constraintTop_toBottomOf="@id/userLevelProgress" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

I can't find the problem in my code that causes the fragment under the ViewPager being unscrollable.

Upvotes: 0

Views: 905

Answers (2)

Ferran
Ferran

Reputation: 1448

Rty this

In your ViewPager

 <androidx.viewpager.widget.ViewPager
        android:id="@+id/userInfoViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"                          // <- ****
        app:layout_constraintBottom_toBottomOf="parent"      // <- ****
        app:layout_constraintTop_toBottomOf="@id/tablayout"
 />

Then, in fragment xml

<ScrollView
    ....
    android:layout_width="match_parent"
    android:layout_height="match_parent"  // <- ****
   >

   <androidx.constraintlayout.widget.ConstraintLayout
        android:padding="40dp"
        android:id="@+id/userInfoContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" // <- ****
    >

Upvotes: 1

ismail alaoui
ismail alaoui

Reputation: 6063

it's seems like there some bug with ConstraintLayout inside ScrollView ,Don't forget that If you constraint some view's bottom to constraint layout's bottom. Scrollview could not scroll. Use NestedScrollView with viewport true is working good for me

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

Upvotes: 0

Related Questions