You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Collapsing Toolbar Layout搭配barteksc PDF Viewer滚动异常求助

解决CollapsingToolbarLayout搭配PDFView的滚动冲突问题

我之前也踩过这个一模一样的坑!问题根源在于NestedScrollView和PDFView的滚动事件冲突:PDFView本身已经实现了完整的滚动逻辑,把它套在NestedScrollView里后,滚动事件会被NestedScrollView先拦截消费,导致只有CollapsingToolbarLayout能正常收缩,而PDF内容完全无法滚动。

下面给你两种解决方案,优先推荐第一种,简单高效:

方案一:移除NestedScrollView,直接让PDFView与CoordinatorLayout交互

把包裹PDFView的NestedScrollView去掉,让PDFView直接作为CoordinatorLayout的子View,同时开启嵌套滚动支持,并设置对应的滚动行为。修改后的布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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"
    android:fitsSystemWindows="true"
    tools:context=".activity.showArticle">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="Deprission">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@drawable/depression"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

                <ImageView
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/back_icon" />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <!-- 直接让PDFView作为CoordinatorLayout的子View -->
    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@mipmap/heart_filled"
        app:backgroundTint="@color/appColor"
        app:elevation="6dp"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"
        app:pressedTranslationZ="12dp" />

</android.support.design.widget.CoordinatorLayout>

额外说明:

如果你的PDFView版本较旧,可能默认不支持嵌套滚动,这时候可以在代码里手动开启:

PDFView pdfView = findViewById(R.id.pdfView);
pdfView.setNestedScrollingEnabled(true);

方案二:自定义NestedScrollView(保留嵌套布局时使用)

如果你因为其他布局需求必须保留NestedScrollView,可以自定义一个NestedScrollView,重写触摸事件拦截逻辑,让PDFView的滚动事件不受干扰:

1. 自定义CustomNestedScrollView类

public class CustomNestedScrollView extends NestedScrollView {
    private PDFView pdfView;

    public CustomNestedScrollView(Context context) {
        super(context);
    }

    public CustomNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setPdfView(PDFView pdfView) {
        this.pdfView = pdfView;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (pdfView != null) {
            Rect rect = new Rect();
            pdfView.getHitRect(rect);
            // 当触摸事件在PDFView范围内时,不拦截,交给PDFView处理
            if (rect.contains((int) ev.getX(), (int) ev.getY())) {
                return false;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
}

2. 修改布局文件

把原来的NestedScrollView替换成自定义的CustomNestedScrollView:

<your.package.name.CustomNestedScrollView
    android:id="@+id/custom_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</your.package.name.CustomNestedScrollView>

3. 在代码中绑定PDFView

CustomNestedScrollView scrollView = findViewById(R.id.custom_scroll_view);
PDFView pdfView = findViewById(R.id.pdfView);
scrollView.setPdfView(pdfView);

内容的提问来源于stack exchange,提问作者Zain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:08:22