解决Scrollview包含viewpager的内容不显示,点击tab等切换pager无切换动画,手动滑动pager有切换动画

import android.content.Context;
import androidx.viewpager.widget.ViewPager;

/**
 * 作者: FebMaple on 5/3/2019.
 * 邮箱: [email protected]
 * 版权: 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() 

你可能感兴趣的:(android)