【问题标题】:View with ViewPager not vertically scrollable in ScrollView在 ScrollView 中使用 ViewPager 查看不可垂直滚动
【发布时间】:2018-02-28 19:54:08
【问题描述】:

这感觉很简单,但不知何故我被卡住了。该视图不允许我滚动它。就好像在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 文本持续很长时间,我希望整个屏幕都可以滚动,但事实并非如此。

提前致谢!

【问题讨论】:

  • 只是为了澄清,您是否尝试过任何其他布局?像scrollV中的线性或相对??
  • @SantanuSur 是的,我都试过了。结果相同。但谢谢你的想法。我还用图片更新了问题以澄清我的问题。
  • 但是你在viewpager顶部的空白处做什么..你可以利用它..
  • 应该有一张图片。
  • 做一件事..用nestedScrollView替换scrollView..并在它周围放置一个线性布局..我正在写解决方案..

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


【解决方案1】:

有这样的问题,有时ScrollView 和ViewPager 切换焦点。如果您在ScrollView 中有一个ViewPager,并且您希望它在您触摸它时始终保持焦点,而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.setTabMode(TabLayout.MODE_SCROLLABLE);

【讨论】:

  • 感谢您的回答,但这无济于事。没有任何东西是垂直滚动的(尽管 ViewPager 是水平工作的)。我还用图片更新了问题以澄清观点。
  • 编辑了我的答案,我应该你试试这个。如果它不起作用;尝试使片段内的文本视图(图片上有文本)可滚动,这将是您的解决方法
【解决方案2】:

我最终重写了很多。根据

,滚动到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 中。

感谢其他答案。它可能会帮助其他人!

【讨论】:

  • 绝妙的解决方案!顺便说一句,要使整个内容可滚动(如ScrollView),请添加CollapsingToolbarLayout 并设置其app:layout_scrollFlags="scroll"。
【解决方案3】:

试试用nestedScrollView 代替scrollView..试试下面的sn-p..

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

【讨论】:

  • 谢谢,但运气不好。而且我之前已经尝试过所有变体。我热衷于认为这是 onMeasure 的错,而不是实际的视图层次结构。
猜你喜欢
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多