在使用recyclerView的时候,大多数情况下会使用linearlayoutManager,但是在使用的时候难免会遇到一些问题,比如动态添加时报IndexOutOfBoundsException,或者和scrollview嵌套只显示一行等问题,这个重写后的FullyLinearLayoutManager可以解决使用时遇到的大部分问题。
importandroid.content.Context;
importandroid.support.v7.widget.LinearLayoutManager;
importandroid.support.v7.widget.RecyclerView;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.ViewGroup;
public classFullyLinearLayoutManagerextendsLinearLayoutManager {
private static finalStringTAG= FullyLinearLayoutManager.class.getSimpleName();
publicFullyLinearLayoutManager(Context context) {
super(context);
}
publicFullyLinearLayoutManager(Context context, intorientation, booleanreverseLayout) {
super(context,orientation,reverseLayout);
}
private int[]mMeasuredDimension=new int[2];
@Override
public voidonMeasure(RecyclerView.Recycler recycler,RecyclerView.State state,
intwidthSpec, intheightSpec) {
final intwidthMode = View.MeasureSpec.getMode(widthSpec);
final intheightMode = View.MeasureSpec.getMode(heightSpec);
final intwidthSize = View.MeasureSpec.getSize(widthSpec);
final intheightSize = View.MeasureSpec.getSize(heightSpec);
Log.i(TAG,"onMeasure called.\nwidthMode "+ widthMode
+"\nheightMode "+ heightSpec
+"\nwidthSize "+ widthSize
+"\nheightSize "+ heightSize
+"\ngetItemCount() "+ getItemCount());
intwidth =0;
intheight =0;
for(inti =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) {
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:
}
setMeasuredDimension(width,height);
}
private voidmeasureScrapChild(RecyclerView.Recycler recycler, intposition, intwidthSpec,
intheightSpec, int[] measuredDimension) {
try{
View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
if(view !=null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
intchildWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(),p.width);
intchildHeightSpec = 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{
}
}
@Override
public voidonLayoutChildren(RecyclerView.Recycler recycler,RecyclerView.State state) {
super.onLayoutChildren(recycler,state);
try{
super.onLayoutChildren(recycler,state);
}catch(IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
网友评论