Android NestedScrollView/ScrollView包裹ViewPager自适应高度
当Android的NestedScrollView/ScrollView这类滚动View包裹ViewPager时候,ViewPager中的Fragment包含的又是一系列高度值不固定的View如RecyclerView等等,就会造成ViewPager高度无法自适应子Fragment里面的View的高度,解决方法其中之一就是重新改造ViewPager,让其能自动适应子Fragment里面的View高度,改造后的AutofitHeightViewPager:
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class AutofitHeightViewPager extends ViewPager {
public static final String POSITION = "position";
private int mCurPosition;
private int mHeight = 0;
private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();
private boolean scrollble = true;
public AutofitHeightViewPager(Context context) {
super(context);
}
public AutofitHeightViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mChildrenViews.size() > mCurPosition) {
View child = mChildrenViews.get(mCurPosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
mHeight = child.getMeasuredHeight();
}
}
if (mHeight != 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void updateHeight(int current) {
this.mCurPosition = current;
if (mChildrenViews.size() > current) {
ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (layoutParams == null) {
layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mHeight);
} else {
layoutParams.height = mHeight;
}
setLayoutParams(layoutParams);
}
}
public void setViewPosition(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;
}
}
自适应高度的ViewPager思路是每一次切换时候,重新设置ViewPager的高度值。
使用方法有四点必须实现:
1,先把AutofitHeightViewPager像Android原生的ViewPager一样写进xml布局。
2,在上层Java代码的mViewPager添加的切换事件addOnPageChangeListener里面每次updateHeight,例如:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mViewPager.updateHeight(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
3,构建mViewPager肯定要设置FragmentPagerAdapter,在把创建Fragment喂给FragmentPagerAdapter时候,给Fragment把当前Fragment位置position设置进去,例如:
Fragment xxxFragment = new XXXFragment();
Bundle xxxBunle = new Bundle();
xxxBunle.putInt(AutofitHeightViewPager.POSITION, 1); //1为当前位置,这里的位置索引即为要传递的值,不同的Fragment按照先后添加顺序添加和传递索引
xxxFragment.setArguments(xxxBunle);
mPagerAdapter.addFragment(xxxFragment)
4,然后转入到具体的XXXFragment里面,把当前XXXFragment创建的View高度传回给AutofitHeightViewPager,一般是在onCreateView:
int pos = getArguments().getInt(AutofitHeightViewPager.POSITION);
(强制转换为ViewPager所在的Activity) getActivity()).mViewPager.setViewPosition(view, pos);