Android ScrollView嵌套ViewPager+Fragment时冲突问题解决办法

先上最终效果图( 图像不是很清楚):

这里写图片描述

顶部的广告使用的是ConvenientBanner和本文关系不大。仅做效果展示。

实际上这里使用NestedScrollView 来实现的如上效果。在使用NestedScrollView 嵌套ViewPager的时候出现几个问题:

  • 1.ViewPager 中的Fragment 不显示
  • 2.上下拖动ViewPager 不能垂直方向滚动

1.ViewPager 中的Fragment 不显示

给xml中ScrollView设置android:fillViewport="true" 属性。通过这个就可以让ViewPager中的内容显示。
顾名思义,这个属性允许 NestedScrollView中的组件去充满它。

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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/com_bg_gray_eeeeee"

                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical">

                <com.bigkoo.convenientbanner.ConvenientBanner
                    android:id="@+id/acty_main_cbanner"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/comm_dim_150" />

                <android.support.design.widget.TabLayout
                    android:id="@+id/acty_main_tab"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/comm_dim_40"
                    android:layout_alignParentBottom="true"
                    app:tabBackground="@drawable/selector_tab_bg"
                    app:tabIndicatorHeight="0dp"
                    app:tabSelectedTextColor="@color/com_tv_white_ffffff"
                    app:tabTextAppearance="@style/tab_txtSize"
                    app:tabTextColor="@color/com_tv_black_333333" />

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

            </LinearLayout>


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

2.上下拖动ViewPager 不能垂直方向滚动

最开始使用的默认的ViewPager,发现不能上下滑动,然后通过查找别人的解决办法,NestedScrollView嵌套ViewPager后,如果viewPager中的fragment高度太长,会发现滑动不了,需要自定义ViewPager,测量ViewPager的高度.

这里写图片描述

public class WrapContentHeightViewPager extends ViewPager {
    public WrapContentHeightViewPager(Context context) {
        super(context);  
    }  

    public WrapContentHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }  

    @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);  
    }  
}  

然后将xml布局的ViewPager替换为我们自定义的ViewPager


        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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/com_bg_gray_eeeeee"

                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical">

                <com.bigkoo.convenientbanner.ConvenientBanner
                    android:id="@+id/acty_main_cbanner"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/comm_dim_150" />

                <android.support.design.widget.TabLayout
                    android:id="@+id/acty_main_tab"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/comm_dim_40"
                    android:layout_alignParentBottom="true"
                    app:tabBackground="@drawable/selector_tab_bg"
                    app:tabIndicatorHeight="0dp"
                    app:tabSelectedTextColor="@color/com_tv_white_ffffff"
                    app:tabTextAppearance="@style/tab_txtSize"
                    app:tabTextColor="@color/com_tv_black_333333" />

                <com.aoben.qproj.widget.WrapContentHeightViewPager
                    android:id="@+id/acty_main_vp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </LinearLayout>


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

参考文章:

【Android学习笔记】NestedScrollView嵌套ViewPager后滑动不了的问题

NestedScrollView嵌套ViewPager

ScrollView 嵌套 ViewPager,ViewPager内容不显示问题

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
在使用ViewPagerScrollView嵌套,可能会出现滑动冲突问题解决方法如下: 1. 自定义ViewPager,重写onInterceptTouchEvent方法,处理滑动事件: ```java public class CustomViewPager extends ViewPager { private float mStartX; private float mStartY; public CustomViewPager(Context context) { super(context); } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mStartX = ev.getX(); mStartY = ev.getY(); break; case MotionEvent.ACTION_MOVE: float x = ev.getX(); float y = ev.getY(); float dx = Math.abs(x - mStartX); float dy = Math.abs(y - mStartY); if (dx > dy) { return super.onInterceptTouchEvent(ev); } else { getParent().requestDisallowInterceptTouchEvent(true); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: getParent().requestDisallowInterceptTouchEvent(false); break; } return super.onInterceptTouchEvent(ev); } } ``` 2. 在ScrollView中设置onTouchEvent事件,处理滑动事件: ```java public class CustomScrollView extends ScrollView { private float mStartX; private float mStartY; public CustomScrollView(Context context) { super(context); } public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mStartX = ev.getX(); mStartY = ev.getY(); getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: float x = ev.getX(); float y = ev.getY(); float dx = Math.abs(x - mStartX); float dy = Math.abs(y - mStartY); if (dy > dx) { getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: getParent().requestDisallowInterceptTouchEvent(false); break; } return super.onTouchEvent(ev); } } ``` 3. 在布局文件中使用自定义的ViewPagerScrollView。 ```xml <com.example.CustomViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.viewpager.widget.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.example.CustomViewPager> <com.example.CustomScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... </LinearLayout> </com.example.CustomScrollView> ``` 以上是一种常见的解决滑动冲突问题的方法,在使用需要注意不同的布局会有不同的处理方式,可以根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值