android – 如何在NestedScrollView中使用RecyclerView创建ViewPager

android – 如何在NestedScrollView中使用RecyclerView创建ViewPager,第1张

概述我在ViewPager中使用NestedScrollView. NestedScrollView里面有一个LinearLayout,最后有一些TextViews,TabLayout和ViewPager. TextViews占据了大部分空间,并为ViewPager留下了一点空间. ViewPager使用两个片段,其中一个有几个TextViews和 ImageViews,另一个片段中有一个Recycl 我在VIEwPager中使用nestedScrollVIEw. nestedScrollVIEw里面有一个linearLayout,最后有一些TextVIEws,TabLayout和VIEwPager. TextVIEws占据了大部分空间,并为VIEwPager留下了一点空间. VIEwPager使用两个片段,其中一个有几个TextVIEws和 ImageVIEws,另一个片段中有一个RecyclerVIEw.

当我将VIEwPager的高度设置为WRAP_CONTENT时,它只占用空间,我无法滚动查看第一个片段的其余部分,以及第二个片段scoll里面的小VIEwPager.

例如,当我将VIEwPager的高度设置为1000dp时,我可以向下滚动第一个片段,但第二个片段仍在小型VIEwPager中滚动.在我用第一个Fragment中的RecyclerVIEw scoll滚动片段后不再工作了.

如何修复滚动问题并使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:
@OverrIDeprotected 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全部内容,希望文章能够帮你解决android – 如何在NestedScrollView中使用RecyclerView创建ViewPager所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1138927.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存