Android ScrollView嵌套ViewPager不显示和出现空白部分 解决方法

上效果图

device-2020-06-08-174856

有3种解决方法先大概说一下:

     1.设置ScrollView的fillViewPort为true。然后给viewpager设置固定的高度值。(局限性很大,ScrollView不能跟着一起滑动了,只有viewpager能)。

     2.网上还有一种方法,重写viewpager,但是会留下空白,不留空白的会发现很卡。(亲测)https://blog.csdn.net/qq_27400335/article/details/81116982

     3.自己捣鼓的一套方法,既不留空白,也不会卡。只是转变了一下思路。先说一下,下面帖代码,这种方式也可以解决tablayout的吸顶。  

        这种方法去掉了ScrollView,然后转用了谷歌给我们提供的控件,

CoordinatorLayout  
AppBarLayout
CollapsingToolbarLayout

       通过这三个组合使用,然后再把自己的viewpager拿出来,像往常一样使用即可。既不会出现问题,也不难以理解,倒还可以增加一些动画效果。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
       >
       
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            这里面是设置自己的头部,根据自己的来进行设置
           </LinearLayout>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
           这里是tabLayout。
            <com.qmuiteam.qmui.widget.QMUITabSegment
                android:id="@+id/tabLayout"
                android:layout_marginTop="30dp"
                android:background="@color/colorWhite"
                android:layout_width="match_parent"
                android:layout_height="40dp"/>
    </com.google.android.material.appbar.AppBarLayout>
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

,如有不懂欢迎底部留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值