首页 > 解决方案 > 无法在android中使用滚动条滚动

问题描述

我试图实现 scrollView 并使用滚动条滚动。但是,它不起作用。

这是整个 xml 文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">
    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:scrollbars="vertical"
        android:scrollbarThumbVertical="@color/teal_200"
        android:scrollbarSize="20dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/large_text"/>
    </ScrollView>
</LinearLayout>

java是默认文件。

主要问题

滚动条出现并且可以被用户看到。ScrollBar 也会改变位置。但是,当用户想要使用滚动条滚动时,滚动条没有响应。就像我们无法触摸滚动条一样,而是选择了 textView 将被滑动。

我的研究

在此之前,我确实尝试过实现 GestureDetecter 并浏览了几个链接和谷歌。

  1. 如何在 android WebView 中使用自定义滚动条滚动?

  2. 手势检测和 ScrollView 问题

这些只是与我的问题最相关的两个链接。所以,我也尝试实现 customScrollView 。这导致应用程序崩溃。也使用了已弃用的 api。所以,我没有费心去解决这个问题。相反,我专注于替换该 api 并使该类正常工作。但是,不幸的是,即使我得到了替换它也不起作用。我跳过了该文件,因为它太长了。

标签: androidandroid-scrollview

解决方案


您无需使用ScrollBar

尝试是

在你XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars = "vertical"
            android:text="@string/large_text"/>
  
</LinearLayout>

在你的java中

yourtextview.setMovementMethod(new ScrollingMovementMethod());

推荐阅读