gpt4 book ai didi

android - 我怎样才能在 NestedScrollView 中使用 RecyclerView 工作 ViewPager

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:56 24 4
gpt4 key购买 nike

我将 NestedScrollView 与 ViewPager 一起使用。 NestedScrollView 内部有一个 LinearLayout,最后有一些 TextView、TabLayout 和 ViewPager。 TextViews 占据了大部分空间,而 ViewPager 留下了一点空间。 ViewPager 使用两个 fragment ,其中一个 fragment 有一些 TextViews 和 ImageViews,另一个 fragment 有一个 RecyclerView。

当我将 ViewPager 的高度设置为 WRAP_CONTENT 时,它只占用剩下的空间,我无法滚动查看第一个 fragment 的其余部分,以及小 ViewPager 中的第二个 fragment 。

例如,当我将 ViewPager 的高度设置为 1000dp 时,我可以在第一个 fragment 上向下滚动,但第二个 fragment 仍在小 ViewPager 中滚动。在我使用 RecyclerView scoll 滚动 fragment 后,第一个 fragment 不再工作。

如何解决滚动问题并使 ViewPager 与 WRAP_CONTENT 一起工作?

这是我的代码:

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

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll"
android:fillViewport="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackground"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOME TEXT" />

<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

最佳答案

我认为你应该像这样覆盖 ViewPager:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

关于android - 我怎样才能在 NestedScrollView 中使用 RecyclerView 工作 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33011176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com