我是靠谱客的博主 清新毛巾,最近开发中收集的这篇文章主要介绍viewpager动态改变每一个item的高度,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

viewpager是几乎每个项目都用的 ,有的时候 页面复杂一些需要动态改变每个item的高度,因此需要进行解决。解决方案如下:

首先如果对其进行wrapcontent需要自定义viewpager重写它的onmeasure方法,在里面进行处理 处理之后重新设置viewpager的高度为0  即可解决。看如下代码

/**
 * Created by longcai:zhangyu on 17/3/14.
 */

public class WrapContentViewPager 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 WrapContentViewPager(Context context) {
        super(context);
    }

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



布局里面这样定义

<com.haiying.p2papp.view.WrapContentViewPager
    android:id="@+id/vp_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


for (int i = 0; i < viewList.size(); i++) {

    vp01.setObjectForPosition(viewList.get(i), i);

}

vp01.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        vp01.resetHeight(position);
        switch (position) {
            case 0:
                initTab();
                tvinvest09.setSelected(true);
                tab_1.setVisibility(View.VISIBLE);
                tab_2.setVisibility(View.GONE);
                tab_3.setVisibility(View.GONE);
                tab_4.setVisibility(View.GONE);

                break;

            case 1:
                initTab();
                tvinvest10.setSelected(true);
                tab_1.setVisibility(View.GONE);
                tab_2.setVisibility(View.VISIBLE);
                tab_3.setVisibility(View.GONE);
                tab_4.setVisibility(View.GONE);
                break;
            case 2:
                initTab();
                tvinvest11.setSelected(true);
                tab_1.setVisibility(View.GONE);
                tab_2.setVisibility(View.GONE);
                tab_3.setVisibility(View.VISIBLE);
                tab_4.setVisibility(View.GONE);
                break;
            case 3:
                initTab();
                tvinvest12.setSelected(true);
                tab_1.setVisibility(View.GONE);
                tab_2.setVisibility(View.GONE);
                tab_3.setVisibility(View.GONE);
                tab_4.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

vp01.resetHeight(0);

在改变的时候 调用

vp01.setObjectForPosition(viewList.get(i), i);
这个方法即可

最后

以上就是清新毛巾为你收集整理的viewpager动态改变每一个item的高度的全部内容,希望文章能够帮你解决viewpager动态改变每一个item的高度所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部