【Android】NestedScrollView的简单用法与滚动冲突、滑动冲突

2024-09-07 02:20

本文主要是介绍【Android】NestedScrollView的简单用法与滚动冲突、滑动冲突,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、NestedScrollView

1. 什么是 NestedScrollView

NestedScrollView 是 Android 中一个用于处理垂直方向滚动的布局组件,它继承自 FrameLayout,同时支持嵌套滑动(Nested Scrolling)机制。相比于传统的 ScrollViewNestedScrollView 专为解决嵌套滚动冲突问题设计,能够与其他支持嵌套滑动的子视图(如 RecyclerViewViewPager 等)协同工作。

2. 定义

NestedScrollView 是 Android Jetpack 中的组件,用于容纳能够垂直滚动的视图。当页面布局的内容超过屏幕高度时,可以通过滚动展示全部内容。同时,NestedScrollView 在滚动的过程中与子视图可以进行事件协作。

3. 与ScrollView的区别

NestedScrollViewScrollView 的主要区别在于它具备“嵌套滑动”(Nested Scrolling)功能。在 Android 中,嵌套滑动是一种滚动冲突处理机制,允许父视图和子视图协同工作,共同处理滑动事件。这种机制非常有用,特别是当你在一个滚动视图中嵌套另一个滚动视图时,它能够有效避免滑动冲突。

  • ScrollView:不支持嵌套滑动,通常会出现父子滑动视图的事件冲突,导致滑动体验不佳。
  • NestedScrollView:内置嵌套滑动机制,能够更好地处理父子视图的滚动事件,使页面更加流畅。

4. 常见使用场景

  • 表单页面:在一个页面中,可能会有许多输入框、按钮等,当这些内容超过屏幕时,使用 NestedScrollView 可以让整个页面可滚动。
  • 嵌套滚动视图:当你在一个滚动视图(如 RecyclerView)中嵌套了另一个滚动视图(如 ViewPagerHorizontalScrollView)时,NestedScrollView 能避免滑动事件冲突。
  • CoordinatorLayout 结合NestedScrollView 可以与 CoordinatorLayout 搭配使用,处理如 CollapsingToolbarLayoutAppBarLayout 等复杂的滚动联动效果。

5. 简单使用方法

下面是一个CoordinatorLayout中使用NestedScrollView并嵌套RecyclerView的简单用法:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"android:nestedScrollingEnabled="false" /></LinearLayout></androidx.core.widget.NestedScrollView></androidx.coordinatorlayout.widget.CoordinatorLayout>
  • CoordinatorLayout 是整个布局的容器,用于协调多个滚动视图的交互,尽管当前只涉及了 NestedScrollView
  • NestedScrollView 管理页面的垂直滚动,并能够与其他支持嵌套滚动的视图配合使用。它容纳了 RecyclerView 并负责控制滚动。
  • RecyclerView 展示的是具体的滚动内容,使用 android:nestedScrollingEnabled="false"NestedScrollView 完全负责滚动处理,避免滚动冲突。

二、滚动冲突和滑动冲突

1. 区别

滚动冲突(Scroll Conflict)

​ 滚动冲突发生在嵌套滚动视图中。例如,一个 ScrollView 内部嵌套了一个 RecyclerView,或者一个 ViewPager 内嵌了一个 ScrollView。当用户在一个滚动视图上滑动时,系统需要决定哪个视图应该接收滚动事件,从而可能导致滚动冲突。

滑动冲突(Touch Conflict)

​ 滑动冲突通常发生在多个视图或组件尝试处理相同的触摸事件时。例如,一个 ViewPager 和一个 RecyclerView 都可以响应滑动手势,这会导致滑动冲突。滑动冲突通常涉及触摸事件的处理,而不是滚动事件的嵌套。

2. 处理方式

处理滚动冲突

滚动冲突通常发生在嵌套的滚动视图中,例如在一个 ScrollView 中嵌套了一个 RecyclerView。以下是几种常见的解决方法:

  1. 禁用子视图的嵌套滚动
  • 描述:禁用子视图的嵌套滚动功能,让父视图完全控制滚动行为。

  • 示例

    <androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"android:nestedScrollingEnabled="false" />
    
  • 适用场景:当 RecyclerView 或其他滚动视图嵌套在 ScrollViewNestedScrollView 中时。

  1. 自定义 Behavior
  • 描述:创建自定义的 Behavior 类,控制滚动行为和滚动事件的传递。

  • 示例

    package com.example.nestedscrollviewtest;import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;import androidx.annotation.NonNull;
    import androidx.coordinatorlayout.widget.CoordinatorLayout;
    import androidx.core.view.ViewCompat;
    import androidx.core.widget.NestedScrollView;public class CustomBehavior extends CoordinatorLayout.Behavior<NestedScrollView> {public CustomBehavior() {super();}public CustomBehavior(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull NestedScrollView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {// 控制是否响应嵌套滑动return axes == ViewCompat.SCROLL_AXIS_VERTICAL;}@Overridepublic void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull NestedScrollView child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {// 处理嵌套滑动前的事件}
    }
  • 应用

    <androidx.coordinatorlayout.widget.CoordinatorLayoutandroid: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"><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="com.example.nestedscrollviewtest.CustomBehavior"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="wrap_content"android:nestedScrollingEnabled="false" /></LinearLayout></androidx.core.widget.NestedScrollView></androidx.coordinatorlayout.widget.CoordinatorLayout>

处理滑动冲突

1. 内部拦截(onInterceptTouchEvent

  • 作用:用于拦截触摸事件,决定是否由当前视图处理该事件。
  • 如何工作:在父视图的 onInterceptTouchEvent 方法中,父视图会决定是否拦截事件并交给自己处理。如果返回 true,父视图将处理事件;如果返回 false,事件将传递给子视图处理。

NestedScrollView 中的内部拦截

NestedScrollView 作为一个容器视图,通常会处理其内部的滚动事件。为了确保它能够正确处理滚动事件,可以重写 NestedScrollViewonInterceptTouchEvent 方法来拦截触摸事件,并决定是否让其处理:

package com.example.nestedscrollviewtest;import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;import androidx.core.widget.NestedScrollView;public class CustomNestedScrollView extends NestedScrollView {public CustomNestedScrollView(Context context) {super(context);}public CustomNestedScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// 根据需要判断是否拦截事件// 例如,可以根据事件的类型或滚动方向来决定是否拦截return super.onInterceptTouchEvent(ev);}
}

2. 外部拦截(onTouchEvent

  • 作用:用于处理视图接收到的触摸事件。
  • 如何工作:在子视图的 onTouchEvent 方法中,子视图会处理传递给它的事件。如果子视图无法处理事件,它可以将事件传递给父视图或其他视图。

RecyclerViewonTouchEvent 方法负责处理其自己的触摸事件。通常,你不需要对 RecyclerViewonTouchEvent 进行特别处理,但要确保它正常工作。

public class CustomRecyclerView extends RecyclerView {public CustomRecyclerView(Context context) {super(context);}public CustomRecyclerView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean onTouchEvent(MotionEvent e) {// 默认情况下,RecyclerView 处理自己的触摸事件return super.onTouchEvent(e);}
}

以上就是本篇博客的所有内容


已经到底啦!!

这篇关于【Android】NestedScrollView的简单用法与滚动冲突、滑动冲突的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143785

相关文章

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Java 中 Optional 的用法及最佳实践

《Java中Optional的用法及最佳实践》在Java开发中,空指针异常(NullPointerException)是开发者最常遇到的问题之一,本篇文章将详细讲解Optional的用法、常用方... 目录前言1. 什么是 Optional?主要特性:2. Optional 的基本用法2.1 创建 Opti

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返