NestedScrollView 具有约束布局不滚动
NestedScrollView Having Constraint Layout not scrolling
我的xml是由一个约束布局组成的。这里面有 2 个按钮和 1 个嵌套的滚动视图。在 ScrollView
中有各种元素,包括回收视图。我希望回收视图不滚动,而只滚动嵌套的滚动视图。我在这种模式下禁用了回收视图的滚动
(recyclerView.setNestedScrollingEnabled(false);
)
但问题是嵌套滚动视图不滚动。你能帮助我吗?
我附上下面的代码。
代码以这种模式开始:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/azzera"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="40dp"
android:layout_marginBottom="28dp"
android:background="@drawable/rounded_4lati"
android:backgroundTint="@color/azzurro"
android:elevation="@dimen/dimen8"
android:paddingTop="@dimen/dimen8"
android:text="@string/azzera"
android:textAlignment="center"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/conferma"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="28dp"
android:background="@drawable/rounded_4lati"
android:backgroundTint="@color/azzurro"
android:paddingTop="@dimen/dimen8"
android:text="@string/conferma"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:elevation="@dimen/dimen8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view1" />
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view2" />
<TextView
android:id="@+id/mostraPreferiti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/mostraPreferiti"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view3" />
<TextView
android:id="@+id/creditiEcm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text="@string/creditiECM"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view1" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:inputType="number"
android:hint="@string/zero"
android:text="@string/editText"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/creditiEcm"
app:layout_constraintTop_toTopOf="@+id/view1"
android:autofillHints="" />
<Button
android:id="@+id/buttonMeno"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_remove_circle_azzurro_24dp"
android:gravity="center"
android:textAlignment="gravity"
android:textColor="@color/azzurro"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/buttonPiu"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonPiu"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="32dp"
android:background="@drawable/ic_add_circle_azzurro_24dp"
android:gravity="center"
android:textAlignment="gravity"
android:textColor="@color/azzurro"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/mostraCorsiPassati"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/mostraCorsiPassati"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toTopOf="@+id/view2" />
<Switch
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:theme="@style/SwitchTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/view2" />
<Switch
android:id="@+id/switch_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:theme="@style/SwitchTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/view3" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/professioni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="8dp"
android:text="@string/professione2"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:fontFamily="@font/sanfrancisco"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mostraCorsiPassati" />
<SearchView
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#fff"
android:elevation="2dp"
app:layout_constraintHorizontal_bias="0.557"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/professioni"
app:queryHint="Cerca..">
</SearchView>
<android.support.v7.widget.RecyclerView
android:id="@+id/listProfessioni"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchBar"
android:orientation="vertical"
>
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/professioniNonDisponibili"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/professioniNonDisponibili"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="18sp"
app:autoSizeMaxTextSize="@dimen/dimen20"
app:autoSizeMinTextSize="@dimen/dimen12"
app:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/error" />
<ImageView
android:id="@+id/error"
android:layout_width="@dimen/dimen80"
android:layout_height="@dimen/dimen80"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/ic_error_outline_gray_24dp"
android:contentDescription="@string/TODO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/searchBar"
app:layout_constraintVertical_bias="0.105" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:alpha="1"
android:minWidth="80dp"
android:minHeight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/searchBar"
app:layout_constraintVertical_bias="0.168" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
正如我在评论中回答的那样
对于其他面临同样问题的人。这可能会有所帮助。
由于某些原因,ScrollView
在其中包含 ConstraintLayout
时不会滚动。
对于解决方法,我们可以使用 LinearLayout
或 RelativeLayout
而不是 ScrollView
中的 ConstraintLayout。或者
正如 OP 确实将 ConstraintLayout
放在 Linear or Relative Layout
中一样。
根据 Ali Ahmed 的回答,如果里面有 ConstraintLayout
,ScrollView
不会滚动。那不是真的。我一直在 ScrollView
中使用 ConstraintLayout
并且一切正常。那么您发布的代码的问题是您使用 0dp
作为 RecyclerView
的 layout_height
。 0dp
表示 match_parent
。您应该使用 wrap_content
作为您的 layout_height
。这样 RecyclerView
就可以根据其中的元素扩展它的高度。这样做会使您的代码正常工作。
我的xml是由一个约束布局组成的。这里面有 2 个按钮和 1 个嵌套的滚动视图。在 ScrollView
中有各种元素,包括回收视图。我希望回收视图不滚动,而只滚动嵌套的滚动视图。我在这种模式下禁用了回收视图的滚动
(recyclerView.setNestedScrollingEnabled(false);
)
但问题是嵌套滚动视图不滚动。你能帮助我吗?
我附上下面的代码。
代码以这种模式开始:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/azzera"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="40dp"
android:layout_marginBottom="28dp"
android:background="@drawable/rounded_4lati"
android:backgroundTint="@color/azzurro"
android:elevation="@dimen/dimen8"
android:paddingTop="@dimen/dimen8"
android:text="@string/azzera"
android:textAlignment="center"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/conferma"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="28dp"
android:background="@drawable/rounded_4lati"
android:backgroundTint="@color/azzurro"
android:paddingTop="@dimen/dimen8"
android:text="@string/conferma"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:elevation="@dimen/dimen8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="48dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view1" />
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view2" />
<TextView
android:id="@+id/mostraPreferiti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/mostraPreferiti"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view3" />
<TextView
android:id="@+id/creditiEcm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:text="@string/creditiECM"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view1" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:inputType="number"
android:hint="@string/zero"
android:text="@string/editText"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/creditiEcm"
app:layout_constraintTop_toTopOf="@+id/view1"
android:autofillHints="" />
<Button
android:id="@+id/buttonMeno"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_remove_circle_azzurro_24dp"
android:gravity="center"
android:textAlignment="gravity"
android:textColor="@color/azzurro"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/buttonPiu"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buttonPiu"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="32dp"
android:background="@drawable/ic_add_circle_azzurro_24dp"
android:gravity="center"
android:textAlignment="gravity"
android:textColor="@color/azzurro"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/mostraCorsiPassati"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/mostraCorsiPassati"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toTopOf="@+id/view2" />
<Switch
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:theme="@style/SwitchTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/view2" />
<Switch
android:id="@+id/switch_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:theme="@style/SwitchTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/view3" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/professioni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="8dp"
android:text="@string/professione2"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:fontFamily="@font/sanfrancisco"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mostraCorsiPassati" />
<SearchView
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="#fff"
android:elevation="2dp"
app:layout_constraintHorizontal_bias="0.557"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/professioni"
app:queryHint="Cerca..">
</SearchView>
<android.support.v7.widget.RecyclerView
android:id="@+id/listProfessioni"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchBar"
android:orientation="vertical"
>
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/professioniNonDisponibili"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/professioniNonDisponibili"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="18sp"
app:autoSizeMaxTextSize="@dimen/dimen20"
app:autoSizeMinTextSize="@dimen/dimen12"
app:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/error" />
<ImageView
android:id="@+id/error"
android:layout_width="@dimen/dimen80"
android:layout_height="@dimen/dimen80"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/ic_error_outline_gray_24dp"
android:contentDescription="@string/TODO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/searchBar"
app:layout_constraintVertical_bias="0.105" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:alpha="1"
android:minWidth="80dp"
android:minHeight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/searchBar"
app:layout_constraintVertical_bias="0.168" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
正如我在评论中回答的那样
对于其他面临同样问题的人。这可能会有所帮助。
由于某些原因,ScrollView
在其中包含 ConstraintLayout
时不会滚动。
对于解决方法,我们可以使用 LinearLayout
或 RelativeLayout
而不是 ScrollView
中的 ConstraintLayout。或者
正如 OP 确实将 ConstraintLayout
放在 Linear or Relative Layout
中一样。
根据 Ali Ahmed 的回答,如果里面有 ConstraintLayout
,ScrollView
不会滚动。那不是真的。我一直在 ScrollView
中使用 ConstraintLayout
并且一切正常。那么您发布的代码的问题是您使用 0dp
作为 RecyclerView
的 layout_height
。 0dp
表示 match_parent
。您应该使用 wrap_content
作为您的 layout_height
。这样 RecyclerView
就可以根据其中的元素扩展它的高度。这样做会使您的代码正常工作。