先上效果
自定义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();
}
}
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);
}
}
<?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>
最近在使用mapboxgl实现轨迹展示时,想实现类似高德地图导航轨迹效果,然而并未...
什么是Flyway 转载: https://blog.waterstrong.me/flyway-in-practice/ Flyway ...
SQL上手特别简单,在所有语言里算是简单的一种,甚至很多人不认为它算得上编程语...
'////////////////////////////////////////////////FSO操作///////////////////...
普通spring配置文件模板: xml version="1.0" encoding="UTF-8" beans xmlns:xsi...
一、作用 主要用于保留组件状态或避免重新渲染。 二、用法 keep-alive包裹动态组...
本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz...
由于不打算推出Windows 11,微软的做法是对Windows10进行不断的迭代,为给用户带...
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://...
新建 app\filters\LoggingFilter 继承 yii\base\ActionFilter LoggingFilter 的...