关于ViewPager高度自适应(随着pager页的高度改变Viewpager的高度)

package com.xx.xxxx.views;  

import android.content.Context;  
import android.os.Build;  
import android.support.v4.app.Fragment;  
import android.support.v4.view.ViewPager;  
import android.util.AttributeSet;  
import android.util.TypedValue;  
import android.view.MotionEvent;  
import android.view.View;  
import android.widget.LinearLayout;  

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.LinkedHashMap;  
import java.util.List;  
import java.util.Map;  

/** 
 * Created by vipui on 16/8/25. 
 */  

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;  
    }  

}  

setObjectForPosition()方法中是为了调用存放你的view和他对应的position,这个是参考了鸿洋大神的一篇文章,链接:http://blog.csdn.net/lmj623565791/article/details/38026503,为了防止预加载导致的高度不匹配,我们加自身的fragment和position对应起来放在linkedmap里。
好了,剩下的就是调用
setObjectForPosition()这个方法了,请看我的一个fragment

package com.abo.beauty.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.abo.beauty.R;
import com.abo.beauty.base.BaseFragment;
import com.abo.beauty.view.CustomViewpager;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sk on 17-7-18.
 */

public class ServiceIntroFragment extends BaseFragment {

    private Unbinder unbinder;

    private CustomViewpager viewPager;

    public CustomViewpager getViewPager() {
        return viewPager;
    }

    public void setViewPager(CustomViewpager viewPager) {
        this.viewPager = viewPager;
    }

    static public ServiceIntroFragment newInstance(CustomViewpager vp) {
        ServiceIntroFragment fragment = new ServiceIntroFragment();
        fragment.setViewPager(vp);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_service_intro, container, false);
        unbinder = ButterKnife.bind(this, view);
        viewPager.setObjectForPosition(view, 0);
        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);  

                if (position == 0) {  

                    activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_1);  
                    activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#ffffff"));  
                    activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#c1c1c1"));  
                    activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#c1c1c1"));  

                } else if (position == 1) {  
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {  
//                        activityScdetailsBottomVp.resetHeight(2);  
//                    } else {  
//                        activityScdetailsBottomVp.resetHeight(1);  
//                    }  
                    activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_2);  
                    activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#c1c1c1"));  
                    activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#ffffff"));  
                    activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#c1c1c1"));  
                } else {  

                    activityScdetailsBottomLinear.setBackgroundResource(R.drawable.fishbone_diagram_list_btn_3);  
                    activityScdetailsBottomTaskTv.setTextColor(Color.parseColor("#c1c1c1"));  
                    activityScdetailsBottomInfoTv.setTextColor(Color.parseColor("#c1c1c1"));  
                    activityScdetailsBottomTimeTv.setTextColor(Color.parseColor("#ffffff"));  
                }  
            }  
            @Override  
            public void onPageScrollStateChanged(int state) {  

            }  
        });  
        activityScdetailsBottomVp.resetHeight(0);  


    }  

你可能感兴趣的:(android)