Skip to content

Commit

Permalink
lib update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sum-sdl committed Jun 12, 2018
1 parent a3504aa commit 355e8c6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by sdl on 15/11/27.
Expand All @@ -23,79 +22,9 @@ public FullGridViewManager(Context context, int spanCount) {
public FullGridViewManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, 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);

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) {
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);
}

private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
if (position < getItemCount()) {
try {
View view = recycler.getViewForPosition(0);//fix 动态添加时报IndexOutOfBoundsException
if (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 (IndexOutOfBoundsException e) {
// Log.e("FullGridViewManager", "measureScrapChild recycler get view error ");
} catch (Exception e) {
// e.printStackTrace();
}
}
int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
super.onMeasure(recycler, state, widthSpec, expandSpec);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Step 1. 在build.config文件添加远程库地址
Step 2. 添加远程库依赖

dependencies {
implementation 'com.github.Sum-sdl:RvAdapter:1.0.4'
implementation 'com.github.Sum-sdl:RvAdapter:1.0.5'
}


2 changes: 2 additions & 0 deletions smaple/src/main/java/com/sum/rvadapter/SimpleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SimpleActivity : AppCompatActivity() {

val adapter = RecyclerAdapter<RecyclerDataHolder<*>>()
// rv_simple.layoutManager = LinearLayoutManager(this)
//高度自动计算的布局用来代替部分一次性展示场景
//性能优化,最费时的是view实例化过程
rv_simple.layoutManager = FullGridViewManager(this, 1)
rv_simple.adapter = adapter
rv_simple.isNestedScrollingEnabled = false
Expand Down

0 comments on commit 355e8c6

Please sign in to comment.