【问题标题】:Android click views within a ScrollViewScrollView 中的 Android 点击视图
【发布时间】:2021-01-09 22:55:53
【问题描述】:

我在 ScrollView 的 LinearLayout 中有一个包含 5 个项目的布局。我希望这些项目是可点击的,并且不会干扰滚动。

问题在于滚动。如果向上/向下拖动一个项目,它不会滚动。如果我在项目之间的边距上上下拖动,它会滚动。

如何处理项目的点击事件并在用户向上/向下拖动项目时允许滚动?

注意:这与高效布局或仅使用 RecyclerView 无关(这看起来有点过头了)。我想知道如何滚动和处理被点击的孩子。

我尝试了点击和触摸侦听器、GestureDetector、onInterceptTouchEvent 的各种组合...但我似乎只能点击或滚动,而不是两者。

我遵循了这些教程/答案,但没有一个有用:

一定有一些简单的东西我错过了......一些标志或什么?

布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:id="@+id/rootLayout">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/itemOne"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:margin="16dp" />

            <LinearLayout
                android:id="@+id/itemTwo"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:margin="16dp" />

            ...

活动

override fun onCreate(savedInstanceState: Bundle?) {
   itemOne.setOnClickListener(this)
   itemTwo.setOnClickListener(this)
   ...
}

override fun onClick(view: View?) {
   when (view?.id) {
      R.id.itemOne-> ...
      R.id.itemTwo -> ...
      ...
   }
}

【问题讨论】:

    标签: android click touch scrollview


    【解决方案1】:

    只需删除 XML 中的 scrollView id 以使其在 R 文件中不列出,这将使其在 Listeners 中不可点击

    【讨论】:

    • 这并没有解决问题。问题不在于滚动视图可点击,而是可点击项目消耗滚动触摸事件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多