How to wrap the height of a ViewPager to the height of its current Fragment?

前端 未结 6 1529
轻奢々
轻奢々 2020-11-27 14:04

I made a ScrollView containing a ViewPager, but the ViewPager does not grow in height. When the content inside the ViewPager is too big, it get \'pucht\'(?) inside, the tabl

6条回答
  •  迷失自我
    2020-11-27 14:33

    If these answer still can't fix the problem, I can provide another choice. At the PagerAdapter java file, you can measure the page height manually and set the container's LayoutParams.height in instantiateItem method. Like this:

    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = generateView(mContext, position);
        view.measure(View.MeasureSpec.makeMeasureSpec(DensityUtil.getScreenWidth(mContext), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        container.getLayoutParams().height = view.getMeasuredHeight();
        container.addView(view);
        return view;
    }
    

提交回复
热议问题