android 解决viewpager+多个fragment+recyclerview,viewpager展示内容高度不自适应出现多余空白问题

一、首先要做的就是重写viewpager

public class MyViewPager extends ViewPager {
    private int current;
    private int height = 0;
    /**
     * 保存position与对应的View
     */
    private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();

    private boolean scrollble = true;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(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;
    }

}

二、在父fragment中提供一个公共的方法,给子fragment调用

 /**
     * 提供子fragment调用方法,解决listview高度不自适应出现多余空白
     */
    public void setChildObjectForPosition(View view, int poistion) {
        mViewPagerOrderform.setObjectForPosition(view, poistion);
    }

三、在viewpager的onPageSelected()方法中重置高度

viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewpager.resetHeight(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

四、在子fragment的onCreateView()方法中调用父fragment的方法

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_report_latest,container,false);
    HomeFragment homeFragment = (HomeFragment) getParentFragment();
    homeFragment.setChildObjectForPosition(view,0);
    return view;
}

原文链接:https://blog.csdn.net/qq_33650641/article/details/79483642

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页