如何防止ViewPager横向分页时出现上下可滑动的现象

最近在忙着写UI,用到ViewPager和GridView实现分页,要达到的效果是每页显示一行4列,然后横向滑动实现分页,如下图所示。
None
以下是部分布局文件,使用了ConstraintLayout约束布局,ViewPager控件的高度设置的为parent的46%(注意这个参数很重要,也是问题的根源)。

<android.support.constraint.ConstraintLayout 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.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        **app:layout_constraintHeight_percent="0.46"** //这里的值很重要
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.46" />
        ...

问题就是当用手上下滑动时,仍然存在卡片被拖动的效果,这并不是期望的,经过反复排查,最终发现原因是,由于ViewPager控件中的子布局高度我是在代码中通过设置LayoutParams来实现的,而ViewPager控件布局高度的值估算稍微小于了子布局的高度,这就导致了子布局中的内容有部分无法显示出来,所有就存在了拖动的效果,解决办法就是父布局(ViewPager布局)的高度一定要大于等于其子布局的高度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值