-
先上效果
-
自定义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>
- 其他代码没有改变,参照上篇博客ViewPager系列:解决ViewPager内部,Fragment设置高度不起作用(一)