Android 片段未在viewpager内滚动
我正在使用Android 片段未在viewpager内滚动,android,android-viewpager,android-cardview,Android,Android Viewpager,Android Cardview,我正在使用ViewPager显示4个不同的片段 “我的活动”的组件树: |_FrameLayout |_ LinearLayout (Vertical) Toolbar ViewPager TabLayout RelativeLayout 这是我的第一个片段的组件树: ScrollView |_LinearLayout(Vertical)
ViewPager
显示4个不同的片段
“我的活动”的组件树:
|_FrameLayout
|_ LinearLayout (Vertical)
Toolbar
ViewPager
TabLayout
RelativeLayout
这是我的第一个片段的组件树:
ScrollView
|_LinearLayout(Vertical)
LinearLayout(Horizontal)
|_LinearLayout(Horizontal)
|_CardView
TextView
LinearLayout(Horizontal)
LinearLayout(Horizontal)
但是当设备处于纵向模式时,
Viewpager
内的片段不滚动。在横向模式下,它会滚动一点(因此滚动正常),但cardwiew
中的视图会缩小。cardwiew
是否负责所有这一切(因为它可以滚动)?如何解决这个问题?请提供帮助。您应该将ScrollView替换为一个可以帮助您进行嵌套滚动的视图在滚动视图中同时包装viewpager.xml和fragment.xml,如下所示:
viewpager.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</ScrollView>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Along with other widgets -->
</android.support.constraint.ConstraintLayout>
</ScrollView>
activity.xml
<?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"
tools:context=".MyActivity">
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Top App Bar -->
<RelativeLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<!-- Top Action Bar -->
<include layout="@layout/action_bar" />
<!-- Top Tabs -->
<include layout="@layout/top_tabs" />
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
<!-- Middle Section (Body) -->
<RelativeLayout
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout2">
<include layout="@layout/viewpager" />
</RelativeLayout>
<!-- Bottom Navigation View -->
<include layout="@layout/bottom_navigation_view" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
我尝试了NestedScrollView
但仍然不起作用。您能给我一个工作示例吗?通过在您的viewpager XML中添加android:nestedScrollingEnabled=“true”
来覆盖ViewPagerTry的TouchListener谢谢各位的响应。我找到了原因。我在滚动视图中使用了layout\u weight
。在我使用固定高度后,它可以完美地滚动。我总是使用layou-weight
(好吧,它使ui更具响应性)。现在,当我在滚动视图中实现权重时,为什么会导致灾难?请发布一些代码,你刚刚救了我一天!