有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ScrollView不会在ViewPager中滚动

在我的应用程序中,我使用工具栏下的一个表格布局,以及一个填充其余空间的ViewPager。我需要标签下的一个片段才能滚动。尝试使用ScrollView、NestedScrollView和安卓:fillViewport=“true”但没有成功

我研究了关于同一问题的其他问题,尝试了答案中的任何建议,但它们都没有解决我的问题

包含ViewPager代码的活动:

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

<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    tools:context=".UserProfile">

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

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

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

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

    </com.google.安卓.material.tabs.TabLayout>

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


</安卓x.constraintlayout.widget.ConstraintLayout>

片段的代码:

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

<ScrollView
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/user_dashboard_fragment_container"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:fillViewport="true"
    tools:context=".Dashboard">

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

        <com.github.abdularis.civ.CircleImageView
            安卓:id="@+id/userImage"
            安卓:layout_width="match_parent"
            安卓:layout_height="0dp"
            安卓:padding="20dp"
            安卓:scaleType="fitCenter"
            安卓: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
            安卓:id="@+id/username"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:gravity="center"
            安卓:text="@string/username_sample"
            安卓:textColor="@color/colorPrimaryDark"
            安卓:textSize="30sp"
            app:layout_constraintTop_toBottomOf="@id/userImage" />

        <com.ramijemli.percentagechartview.PercentageChartView
            安卓:id="@+id/userLevelProgress"
            安卓:layout_width="match_parent"
            安卓:layout_height="0dp"
            安卓:layout_margin="20dp"
            安卓: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
            安卓:id="@+id/userLevel"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:gravity="center"
            安卓:text="@string/level_sample"
            安卓:textColor="@color/colorPrimary"
            安卓:textSize="40sp"
            app:layout_constraintTop_toBottomOf="@id/userLevelProgress" />


    </安卓x.constraintlayout.widget.ConstraintLayout>

</ScrollView>

我在代码中找不到导致ViewPager下的片段不可滚动的问题


共 (1) 个答案

  1. # 1 楼答案

    这个

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