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

该博客借鉴的是某位大神的博客 我只是写一下用后感和总结 博客链接地址 http://blog.csdn.net/qq_34972666/article/details/52386999?locationNum=11

首先当然是重写ViewPager啦 直接粘代码:

public class CustomViewpager 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 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;
} }

这里应该注意的是

resetHeight()的方法要根据自己实际项目的父布局来写LinearLayout还是RelativeLayout

setObjectForPosition()方法中是为了调用存放你的view和他对应的position

然后就是我们的的Fragment的代码 我们创建的Fragment的时候需要在构造方法中传入我们自定义的ViewPager对象  然后在onCreateView的方法中调用
setObjectForPosition() 下面吧Fragment的代码粘出来
public class NewPersonDataAssetPage extends BaseFragment {

    private CustomPersonViewPager vp;
private Activity activity; public NewPersonDataAssetPage(Activity activity, CustomPersonViewPager vp) {
this.activity = activity;
this.vp = vp;
} @Override
protected View initView(LayoutInflater inflater, ViewGroup container) {
View view = View.inflate(activity, R.layout.fragment_new_person_asset_page, null);
vp.setObjectForPosition(view, 1);
return view;
} @Override
protected void initData() { } @Override
public void onClick(View v) { }
}

最后就是要在ViewPager滑动的监听里面去写

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);
上一篇:并发编程—— LinkedTransferQueue


下一篇:Samba-ADS/WINBIND