第一种方法:固定高度
如果你要是知道ViewPager的高度是固定的话,那就好办了,直接固定写死就行,简单粗暴
第二种方法:自定义ViewPager
ViewPagerForScrollView
package com.lycompany.health.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import androidx.viewpager.widget.ViewPager;
/**
* Create by Mazhanzhu on 2020/9/2
* 去除viewpager本身的滑动效果,并在ScrollView中使用本控件
*/
public class ViewPagerForScrollView extends ViewPager {
public ViewPagerForScrollView(Context context) {
super(context);
}
public ViewPagerForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
//去除页面切换时的滑动翻页效果
super.setCurrentItem(item, false);
}
}
然后就可以直接使用了,但是注意!我的自定义里面把它本身的切换效果去掉了,如果保留的话,记得注释掉~