android - 使用 ViewPager 查看在 ScrollView 中不能垂直滚动

标签 android android-viewpager android-constraintlayout android-nestedscrollview

这感觉很简单,但不知何故我被困住了。该 View 不允许我滚动它。就好像没有计算在 ViewPager 中膨胀的 fragment 的高度。搜索 SO 表明我需要编写自己的 ViewPager 并覆盖 onMeasure(),但我不敢相信我会为了这么简单的事情走那么远。

这是我的布局,为方便起见进行了一些简化。 ViewPager 填充了占据屏幕以外空间的 fragment (垂直)。尽管如此,屏幕上的任何内容都不可滚动。这是为什么?

<ScrollView
    android:id="@+id/scroll_view"
    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:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/header_image_view"
            android:layout_width="0dp"
            android:layout_height="170dp"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/title_text_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/header_image_view"/>

        <Button
            android:id="@+id/resume_training_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/title_text_view"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/lesson_table_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"          
            app:layout_constraintTop_toBottomOf="@id/resume_training_button">
        </android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/lesson_table_layout">
        </android.support.v4.view.ViewPager>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

这是一个示例 View 。 Lorem ipsum 文本会持续很长时间,我希望整个屏幕都可以滚动,但事实并非如此。

example_view

提前致谢!

最佳答案

存在这样的问题,有时ScrollViewViewPager切换焦点。

If you have a ViewPager in a ScrollView and you want it always to stay in focus when you touch it and the ScrollView never getting a focus, setting the requestDisallowInterceptTouchEvent does that.

 mViewPager= (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(adapter);
    mViewPager.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mViewPager.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

在此 thread 中查找更多答案

编辑:

我认为另一种可行的方法是以编程方式设置滚动到您的 tablayout

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

关于android - 使用 ViewPager 查看在 ScrollView 中不能垂直滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49037539/

相关文章:

android - ApolloGraphql 文件未找到异常 : source/apollo/generatedIR/debug/src (Is a directory)

android - 在 ConstraintLayout 中的 ViewPager 中使用 RecyclerView

android - Recyclerview 和 Cardview 我有设计问题

java - 在 View 寻呼机中加载网页 View 的时间太多

android - 如何以编程方式调用 ViewPager 的下一页?

java - 如果 2 TextView 在 ConstraintLayout 中有长文本,如何缩小第一个 TextView 而不是第二个?

android - Constraintlayout 中 recyclerview 的行项目折叠而不是采用父宽度

android - cordova 平台添加 android - 不适用于 Mac Os

android - 动画相对布局以像 Snack Bar 一样向下/向上移动

android - Linux X86 或 Mac 构建上的 Dalvik JIT 编译器