解决XRecyclerView里面嵌套viewpager时不显示的问题

参考了这篇文章  作者 郭志豪  链接:https://blog.csdn.net/qq_27400335/article/details/81116982

 

我的需求是在头部用代码添加viewpager,里面存放fragment列表

1. 首先重写viewpager:

public class MyViewPager extends ViewPager{

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

 

2. 然后给viewpager设置id

mViewPager.setId(id);

之后就显示正常了

你可能感兴趣的:(开发经验)