无法滚动recyclerview

时间:2019-06-02 17:39:47

标签: android android-recyclerview scroll

我正在尝试实现Recyclerview,但是我无法使其滚动。我在StackOverflow中阅读了多个线程,但是找不到解决该问题的线程。我认为与MapView有关,因为我还有另一个类似的活动,没有可以滚动的地图,但是在这种情况下我仍然不知道如何解决。

<?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"
  tools:context=".activities.BookmarkActivity">

  <com.google.android.gms.maps.MapView android:id="@+id/bk_mapView" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintHeight_default="percent" app:layout_constraintHeight_percent="0.4" android:layout_marginTop="0dp" app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" />

  <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/bk_linearlayout" android:visibility="visible" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bk_mapView">

    <TextView android:id="@+id/radlabel" android:layout_width="wrap_content" android:layout_height="25dp" android:layout_marginStart="4dp" android:layout_marginTop="2dp" android:layout_marginBottom="8dp" android:hint="@string/bk_radiusName" android:textAlignment="center"
      android:textSize="15sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/bk_textName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

    <SeekBar android:id="@+id/bk_seekBar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:layout_marginEnd="8dp" android:max="1000" android:min="10" android:progress="50" app:layout_constraintEnd_toStartOf="@+id/bk_imageButton"
      app:layout_constraintHorizontal_weight="1" app:layout_constraintStart_toEndOf="@+id/radlabel" app:layout_constraintTop_toTopOf="parent" />


    <ImageButton android:id="@+id/bk_imageButton" android:layout_width="25dp" android:layout_height="25dp" android:layout_marginEnd="8dp" android:background="@android:drawable/ic_input_add" app:layout_constraintEnd_toEndOf="parent" />

    <EditText android:id="@+id/bk_textName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="2dp" android:layout_marginTop="4dp" android:layout_marginEnd="8dp" android:hint="@string/bk_bookmarkName" android:inputType="textCapWords"
      android:maxLength="30" android:textSize="15sp" app:layout_constraintEnd_toStartOf="@+id/bk_notify" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bk_seekBar" />

    <CheckBox android:id="@+id/bk_notifyWifiOnly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WifiOnly" android:visibility="gone" android:layout_marginTop="4dp" android:layout_marginEnd="4dp" android:layout_marginBottom="2dp"
      app:layout_constraintBottom_toBottomOf="@+id/bk_textName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/bk_seekBar" />

    <CheckBox android:id="@+id/bk_notify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notify" android:layout_marginTop="4dp" android:layout_marginEnd="4dp" android:layout_marginBottom="2dp" app:layout_constraintBottom_toBottomOf="@+id/bk_textName"
      app:layout_constraintEnd_toStartOf="@+id/bk_notifyWifiOnly" app:layout_constraintTop_toBottomOf="@+id/bk_seekBar" />

  </androidx.constraintlayout.widget.ConstraintLayout>


  <FrameLayout android:id="@+id/emptyFrameLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="2dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bk_linearlayout">

    <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="2dp" android:layout_marginTop="4dp" android:textAlignment="center" android:layout_marginBottom="150dp" android:textSize="20sp" android:textStyle="bold|italic"
      android:text="None Here" app:layout_constraintTop_toTopOf="parent" />

  </FrameLayout>


  <FrameLayout android:id="@+id/topFrameLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:layout_marginEnd="2dp" android:visibility="visible" app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bk_linearlayout">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/bk_swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="wrap_content">

      <androidx.recyclerview.widget.RecyclerView android:id="@+id/bk_recycler_view" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

  </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

实际的屏幕截图在这里:

enter image description here

1 个答案:

答案 0 :(得分:0)

RecyclerView放在ScrollView中。 SwipeRefreshLayout扩展了ViewGroup,用于允许用户通过垂直滑动手势刷新/更新视图的内容。因此,它可以检测到用户做出的垂直滑动手势,但它本身的内,外并不能充当滚动视图。

您的代码应转换为以下几行:

<FrameLayout
        android:id="@+id/topFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="2dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bk_linearlayout">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/bk_swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/bk_recycler_view"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical" />

            </ScrollView>

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </FrameLayout>

要获得有关如何使用SwipeRefreshLayout的基本知识,请参阅此article。另外,请参考official documentation

有关SwipeRefreshLayout的摘要(来自官方文档)

  

每当用户可以通过垂直滑动手势刷新视图内容时,都应使用SwipeRefreshLayout。实例化此视图的活动应添加一个OnRefreshListener,以在完成刷新手势时得到通知。每次手势再次完成时,SwipeRefreshLayout都会通知侦听器。侦听器负责正确确定何时实际开始刷新其内容。如果侦听器确定不应进行刷新,则必须调用setRefreshing(false)取消刷新的任何可视指示。如果活动仅希望显示进度动画,则应调用setRefreshing(true)。要禁用手势和进度动画,请在视图上调用setEnabled(false)。

     

此布局应作为视图的父级,该视图将由于手势而刷新,并且只能支持一个直接子级。该视图也将成为手势的目标,并且将被迫匹配此布局中提供的宽度和高度。 SwipeRefreshLayout不提供可访问性事件。而是必须提供一个菜单项,以允许在使用此手势的任何地方刷新内容。