簡體   English   中英

ScrollView不在ViewPager中滾動

[英]ScrollView doesn't scroll in a ViewPager

在我的應用程序中,我使用工具欄下的TabLayout,並使用ViewPager填充其余空間。 我需要選項卡下的片段之一才能滾動。 嘗試使用ScrollView,NestedScrollView和android:fillViewport =“ true”失敗。

我查看了關於同一問題的其他問題,嘗試了答案中提出的所有建議,但都沒有解決我的問題。

包含ViewPager代碼的活動:

<?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>

片段的代碼:

<?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>

我在代碼中找不到導致ViewPager下的片段無法滾動的問題。

Rty這個

在您的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"
 />

然后,在片段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" // <- ****
    >

似乎在ScrollView存在ConstraintLayout一些錯誤,請不要忘記,如果您將某個視圖的底部約束到約束布局的底部。 Scrollview無法滾動。 NestedScrollView與true視口一起使用對我來說很好

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM