import android.content.Context;
import androidx.viewpager.widget.ViewPager;
/**
* 作者: FebMaple on 5/3/2019.
* 邮箱: febmaple219@gmail.com
* 版权: FebMaple
* ====================================================
*/
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);
}
//用于点击tab等切换pager无切换动画,手动滑动pager有切换动画
@Override
public void setCurrentItem(int item) {
if (hasFocus()) {
super.setCurrentItem(item, true);
} else {
super.setCurrentItem(item, false);
}
}
}
//PS:
//是否当前视图就是焦点视图
public boolean isFocused()
//当前视图是否是焦点视图,或者子视图里面有焦点视图。
public boolean hasFocus()
解决Scrollview包含viewpager的内容不显示,点击tab等切换pager无切换动画,手动滑动pager有切换动画
最新推荐文章于 2024-03-28 09:13:00 发布