ViewPager中的视图在ScrollView中不能垂直滚动

问题描述 投票:2回答:3

这感觉就像一个没脑子但不知何故我被卡住了。该视图不允许我滚动它。就好像在ViewPager中膨胀的片段的高度没有得到计算。搜索SO建议我需要编写自己的ViewPager并覆盖onMeasure(),但我不敢相信我必须走这么远的东西这么简单。

这是我的布局,为方便起见略有简化。 ViewPager的片段占据了屏幕之外的空间(垂直)。尽管如此,屏幕上的任何内容都无法滚动。这是为什么?

<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>

这是一个示例视图。 Lorem ipsum文本持续了很长时间,我期待整个屏幕可以滚动,但事实并非如此。

example_view

提前致谢!

android android-viewpager android-constraintlayout android-nestedscrollview
3个回答
1
投票

有这样的问题,有时ScrollViewViewPager切换焦点。如果你在ViewPager中有一个ScrollView并且你希望它总是在你触摸时保持焦点并且ScrollView从未得到焦点,那么设置requestDisallowInterceptTouchEvent就是这样。

 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);

0
投票

尝试使用nestedScrollView而不是scrollView ..尝试以下代码段..

<android.support.v4.widget.NestedScrollView 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@id/fragment_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:orientation="vertical">
    ...Content...
     
     <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>
   
     </LinearLayout>
</android.support.v4.widget.NestedScrollView>

0
投票

我最后重写了一下。在TabLayout上面滚动是没有意义所以我最终使用了CoordinatorLayout,根据

<CoordinatorLayout>        <-- width/height = match_parent
    <AppBarLayout>         <-- width = match_parent, height = wrap_content
        <ImageView/>
        <TextView/>
        <Button/>
        <TabLayout/>
    <AppBarLayout/>
    <ViewPager/>           <-- width/height = match_parent, app:layout_behavior="@string/appbar_scrolling_view_behavior"
<CoordinatorLayout/>

插入ViewPager的每个片段现在都包裹在NestedScrollView中。

谢谢你的其他答案。它可能会帮助别人!

© www.soinside.com 2019 - 2024. All rights reserved.