scrollview嵌套ViewPager的冲突处理

场景解释:为了能使整个Activity界面能够上下滑动,使用了ScrollView,将Tablayout和ViewPager的联合包裹在RelativeLayout中,作为一部分。
遇到问题:
1.正常使用情况下,ViewPager中的Fragment没有显示出来,在ScrollView的布局参数上添加了 android:fillViewport="true",结果是ViewPager中的Fragment可以正常显示了。
2.Fragment的正常显示的同时还有个问题就是,ScrollView不能上下滑动滑动,Fragment中使用的是RecyclerView,在Fragment中的RecyclerView是可以滑动的。ViewPager高度使用的wrap_content,高度不能自适应,必须指定高度才能自适应。解决如下:

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyViewPager extends ViewPager {
    private static final String TAG = MyViewPager.class.getName();

    public MyViewPager(Context context) {
        super(context);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        Log.e(TAG, "getChildCount()=" + getChildCount() + "");
        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);
    }
}

原文链接:https://www.jianshu.com/p/f6493e31f689

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容