ViewPager系列:解决ViewPager内部,Fragment设置高度不起作用(二)

  • 先上效果

  • 自定义ViewPager,CustomViewPager代码如下。

public class CustomViewPager extends ViewPager {

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

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

    int mCurrentPagePosition;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
            if (wrapHeight) {
                View child = getChildAt(mCurrentPagePosition);
                if (child != null) {
                    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();

                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void reMeasureCurrentPage(int position) {
        mCurrentPagePosition = position;
        requestLayout();
    }
}
  • MainActivity代码修改,添加切换监听。并且ViewPager设置setOffscreenPageLimit()。
public class MainActivity extends AppCompatActivity {
    private Fragment fragment1, fragment2, fragment3;
    CustomViewPager vp;
    Fragment[] fragments = new Fragment[3];
    String[] nameList = new String[]{"课堂报告","学习记录","其他"};
    VPFragmentAdapter adapter;
    SlidingTabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = findViewById(R.id.vp);
        tabLayout = findViewById(R.id.tabs);

        fragment1 = new Test1Fragment();
        fragment2 = new Test2Fragment();
        fragment3 = new Test3Fragment();

        fragments[0]=fragment1;
        fragments[1]=fragment2;
        fragments[2]=fragment3;

        adapter = new VPFragmentAdapter(getSupportFragmentManager(), fragments,nameList);
        vp.setOffscreenPageLimit(fragments.length); //解决报空问题
        vp.setAdapter(adapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vp.reMeasureCurrentPage(vp.getCurrentItem());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tabLayout.setViewPager(vp,nameList);
    }
}
  • activity_main.xml代码修改
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000">


        <com.flyco.tablayout.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_alignParentTop="true"
            android:background="#ffff00"
            app:tl_indicator_color="#4F9AFF"
            app:tl_indicator_corner_radius="3dp"
            app:tl_indicator_gravity="BOTTOM"
            app:tl_indicator_height="4dp"
            app:tl_indicator_margin_bottom="8dp"
            app:tl_indicator_width="40dp"
            app:tl_tab_space_equal="true"
            app:tl_textBold="BOTH"
            app:tl_textSelectColor="#ff313543"
            app:tl_textUnselectColor="#99313543"
            app:tl_textsize="15sp" />

        <com.zj.viewpagerdemo.CustomViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/tabs"
            android:background="#ffffff" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangjin1120

可靠的文章费时费力,希望支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值