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

  • 复现一个bug,三个Fragment设置的高度分别是50dp,100dp,150dp,但是最终效果都是全屏,如下。

  • 先导入FlycoTabLayout依赖库,因为Demo使用到了SlidingTabLayout,不清楚的可以参考:TabLayout系列:FlycoTabLayout实现点击切换效果

 implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
  • 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" />

        <androidx.viewpager.widget.ViewPager
            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>
  • MainActivity.java代码
public class MainActivity extends AppCompatActivity {
    private Fragment fragment1, fragment2, fragment3;
    ViewPager 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.setAdapter(adapter);
        tabLayout.setViewPager(vp,nameList);
    }
}
  • VPFragmentAdapter代码
public class VPFragmentAdapter extends FragmentPagerAdapter {
    private String[] mTitle;
    private Fragment[] fragments;

    public VPFragmentAdapter(FragmentManager fm, Fragment[] fragments, String[] pageNames) {
        super(fm);
        this.fragments = fragments;
        this.mTitle = pageNames;
    }

    @Override
    public Fragment getItem(int i) {
        return fragments[i];
    }


    @Override
    public int getCount() {
        return fragments.length;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
    }
}
  • Test1Fragment代码如下,其他Fragment代码类似。
public class Test1Fragment extends Fragment {

    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_test, container, false);
        return rootView;
    }
}
  • fragment_test.xml代码如下,另外两个Fragment的布局代码只是颜色、高度不同。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#00ff00"
    tools:context=".Test1Fragment">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="test1" />

</RelativeLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值