Android

Android NestedScrollView嵌套ViewPa

2020-09-17  本文已影响0人  可乐_JS
问题:今天在写一个NestedScrollView嵌套TabLayout+ViewPager+Fragment页面的时候,写完只有一个Tablayout,没有ViewPager,当时害怕极了~

度了一下,说是在NestedScrollView布局中添加参数 android:fillViewport="true"可以解决,赶紧试了下,结果并没有解决我的问题!

有一位网友是这样描述的:

先前在低于Android 9 以下版本开发的时候 在NestedScrollView 控件属性中加入android:fillViewport=“true” 充满布局属性还起作用,之后就无效了。
可是我试了下9以下的好像也是不行的,这问题不用纠结,就算是9以下可以那也得适配啊,go on

最终还是走了自定义

自定义ViewPager,重写onMeasure,通过在VP中记录每个Fragment创建完成后对应的View,然后根据对应Fragment的View的高度来重置ViewPager的高度。代码如下:

/**
 * Des:
 * Created by kele on 2020/9/17.
 * E-mail:984127585@qq.com
 */
public class AutoHeightViewPager 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 AutoHeightViewPager(Context context) {
        super(context);
    }

    public AutoHeightViewPager(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) {
            ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new ViewGroup.LayoutParams(ViewGroup.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代码如下:

/**
 * Des:
 * Created by kele on 2020/9/17.
 * E-mail:984127585@qq.com
 */
public class DetailFragment extends BaseFragment {

    private int mPosition;
    private AutoHeightViewPager mVP;

    public DetailFragment (AutoHeightViewPager vp, List<ListBean> list, int position) {
        super(true);
        mVP = vp;
        mList = list;
        mPosition = position;
    }

    @Override
    protected IBaseP createPresenter() {
        return null;
    }

    @Override
    protected int getContentViewID() {
        return R.layout.fragment_huati_detail;
    }

    @Override
    protected void initViews(View v) {
        mVP.setObjectForPosition(v, mPosition);
    }

    @Override
    protected void initData() {

    }
}

对应Activity中的操作代码如下:

        vp.resetHeight(0);
        //添加viewpager 切换监听
        stl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.resetHeight(position);
            }

            @Override
            public void onTabReselect(int position) {

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

            }

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

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

参考链接

上一篇下一篇

猜你喜欢

热点阅读