这感觉很简单,但不知何故我被困住了。该 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 文本会持续很长时间,我希望整个屏幕都可以滚动,但事实并非如此。
提前致谢!
最佳答案
存在这样的问题,有时ScrollView
和ViewPager
切换焦点。
If you have a
ViewPager
in aScrollView
and you want it always to stay in focus when you touch it and theScrollView
never getting a focus, setting therequestDisallowInterceptTouchEvent
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/