标签:scrollview android 滑动 嵌套
scrollview 嵌套recyclerview 时,recyclerview不显示,这就需要我们自己计算recyclerview的高度,比如:
ViewGroup.LayoutParams mParams = recyclerView.getLayoutParams();
mParams.height = (CommonUtils.getScreenWidthPX(getActivity()) * 480 / 720 + CommonUtils.dipToPixels(40)) * num + CommonUtils.dipToPixels(8);
mParams.width = CommonUtils.getScreenWidthPX(getActivity());
recyclerView.setLayoutParams(mParams);
这中方法适合item高度比较好计算的情形,但要遇到里面的item高度不一定这就需要我们重写recyclerview的高度了,以前嵌套listview的时候我们只需重写listview 然后重写
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
但是这种方法在recyclerview重写不管用。
我们此时要重写的的是LinearLayoutManager或GridLayoutManager
public class FullyLinearLayoutManager extends LinearLayoutManager {
private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();
public FullyLinearLayoutManager(Context context) {
super(context);
}
public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
+ " \nheightMode " + heightSpec
+ " \nwidthSize " + widthSize
+ " \nheightSize " + heightSize
+ " \ngetItemCount() " + getItemCount());
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:}
setMeasuredDimension(width, height);}privatevoidmeasureScrapChild(RecyclerView.Recycler recycler,int position,int widthSpec,int heightSpec,int[] measuredDimension){try{View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsExceptionif(view !=null){RecyclerView.LayoutParams p =(RecyclerView.LayoutParams) view.getLayoutParams();int childWidthSpec =ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft()+ getPaddingRight(), p.width);int childHeightSpec =ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop()+ getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0]= view.getMeasuredWidth()+ p.leftMargin + p.rightMargin;
measuredDimension[1]= view.getMeasuredHeight()+ p.bottomMargin + p.topMargin;
recycler.recycleView(view);}}catch(Exception e){
e.printStackTrace();}finally{}}}
=======
public class FullyGridLayoutManager extends GridLayoutManager {
private int mwidth = 0;
private int mheight = 0;
public FullyGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public FullyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
private int[] mMeasuredDimension = new int[2];
public int getMwidth() {
return mwidth;
}
public void setMwidth(int mwidth) {
this.mwidth = mwidth;
}
public int getMheight() {
return mheight;
}
public void setMheight(int mheight) {
this.mheight = mheight;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
int count = getItemCount();
int span = getSpanCount();
for (int i = 0; i < count; i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
if (i % span == 0) {
width = width + mMeasuredDimension[0];
}
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
if (i % span == 0) {
height = height + mMeasuredDimension[1];
}if(i ==0){
width = mMeasuredDimension[0];}}}switch(widthMode){caseView.MeasureSpec.EXACTLY:
width = widthSize;caseView.MeasureSpec.AT_MOST:caseView.MeasureSpec.UNSPECIFIED:}switch(heightMode){caseView.MeasureSpec.EXACTLY:
height = heightSize;caseView.MeasureSpec.AT_MOST:caseView.MeasureSpec.UNSPECIFIED:}
setMheight(height);
setMwidth(width);
setMeasuredDimension(width, height);}privatevoidmeasureScrapChild(RecyclerView.Recycler recycler,int position,int widthSpec,int heightSpec,int[] measuredDimension){if(position < getItemCount()){try{View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsExceptionif(view !=null){RecyclerView.LayoutParams p =(RecyclerView.LayoutParams) view.getLayoutParams();int childWidthSpec =ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft()+ getPaddingRight(), p.width);int childHeightSpec =ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop()+ getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0]= view.getMeasuredWidth()+ p.leftMargin + p.rightMargin;
measuredDimension[1]= view.getMeasuredHeight()+ p.bottomMargin + p.topMargin;
recycler.recycleView(view);}}catch(Exception e){
e.printStackTrace();}}}}
重写完之后,用就好说了,在adapter的onBindview和平常一样用就可以了
final FullyGridLayoutManager manager = new FullyGridLayoutManager(context.getActivity(), 3);
manager.setOrientation(GridLayoutManager.VERTICAL);
manager.setSmoothScrollbarEnabled(true);
viewHolder.recyclerView.setLayoutManager(manager);
此种方法在4.x系统上好用,能显示滑动也流畅,但是在5.x上虽然显示正常,但是滑动的时候好像被粘住了,没有惯性效果。。。。然后郁闷了一下午。。。。
最后解决方法是重写最外层的Scrollview
**
* 屏蔽 滑动事件
* Created by fc on 2015/7/16.
*/
public class MyScrollview extends ScrollView {
private int downX;
private int downY;
private int mTouchSlop;
public MyScrollview(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) e.getRawX();
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}
这样就可以了,暴力屏蔽。。。。5以上的事件直接传递给了内层的recyclerview,所以我们把滑动事件拦截就好了。。。