解决viewPager+Fragment会出现viewpager中的fragment高度不正常的问题

详情见:https://blog.csdn.net/fzkf9225/article/details/79963541


public class CustomViewpager extends ViewPager {
    private int current;
    private int height = 0;
    /**
     * 保存position与对于的View
     */
    private HashMap mChildrenViews = new LinkedHashMap();
 
    private boolean scrollble = true;
 
    public CustomViewpager(Context context) {
        super(context);
    }
 
    public CustomViewpager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mChildrenViews.size() > current) {
            View child = mChildrenViews.get(current);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = child.getMeasuredHeight();
        }
 
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
 
    public void resetHeight(int current) {
        this.current = current;
        if (mChildrenViews.size() > current) {
 
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
            } else {
                layoutParams.height = height;
            }
            setLayoutParams(layoutParams);
        }
    }
    /**
     * 保存position与对于的View
     */
    public void setObjectForPosition(View view, int position)
    {
        mChildrenViews.put(position, view);
    }
 
 
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!scrollble) {
            return true;
        }
        return super.onTouchEvent(ev);
    }
 
 
    public boolean isScrollble() {
        return scrollble;
    }
 
    public void setScrollble(boolean scrollble) {
        this.scrollble = scrollble;
    }
 
}
public SecurityInfoFragment(CustomViewpager vp) {
        this.vp = vp;
    }
 
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_sc_filght_info, null);
        ButterKnife.bind(this, view);
        vp.setObjectForPosition(view,1);//添加这句(1代表的是该Fragment对象在ViewPager的位置)
        return view;
    }
activityScdetailsBottomVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 
            }
 
            @Override
            public void onPageSelected(int position) {
                activityScdetailsBottomVp.resetHeight(position);//每次切换页面,都重新重置高度
           }
});
  activityScdetailsBottomVp.resetHeight(0);

你可能感兴趣的:(解决viewPager+Fragment会出现viewpager中的fragment高度不正常的问题)