在最近的项目中,要用到ViewPager,但是viewpager是放在recycleview里面,高度是不确定的,只好重写measure方法来根据子布局的高度来动态生成viewpager的高度,但是有个缺点,就是不能根据子布局的高度来收缩viewpager的高度,为了这个优化,在网上找了一个多小时,终于找到个方法,下面直接贴使用的方法:
1.将自定义viewpager放进自己的项目
package com.plyou.leintegration.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import java.util.HashMap;
import java.util.LinkedHashMap;
/**
* Created by admin on 2017/10/25.
*/
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);
if (child != null) {
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;
}
}
2.在fragemnt的oncreatview里面调用viewpager方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_course_comment, container, false);
unbinder = ButterKnife.bind(this, view);
initView();
initData();
return view;
}
private void initView() {
Bundle bundle = getArguments();
courseId = bundle.getString(Constant.COURSE_ID);
position = bundle.getInt(Constant.POSITION);
viewPager.setObjectForPosition(view, position);//调用viewpager的方法
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setNestedScrollingEnabled(false);
commentAdapter = new CourseCommentAdapter(getActivity());
recyclerView.setAdapter(commentAdapter);
}
3.每次滑动viewpager重新设置高度
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
viewPager.resetHeight(position);//重设高度
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});