网格布局的RecyclerView高度随Item自适应
2016-04-13 17:14
387 查看
摘要: 网格布局的RecyclerView高度随Item自适应
表格样式的RecyclerView高度随Item自适应 的解决办法就是自己去重写布局管理器GridLayoutManager。
package com.art.artcircle.widget;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public class WrapHeightGridLayoutManager extends GridLayoutManager {
private int mChildPerLines;
private int[] mMeasuredDimension = new int[2];
public WrapHeightGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
this.mChildPerLines = spanCount;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int height = 0;
for (int i = 0; i < getItemCount(); ) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
height = height + mMeasuredDimension[1];
i = i + mChildPerLines;
}
// If child view is more than screen size, there is no need to make it wrap content. We can use original onMeasure() so we can scroll view.
if (height >heightSize) {
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(widthSize, height);
} else {
super.onMeasure(recycler, state, widthSpec, heightSpec);
}
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
// For adding Item Decor Insets to view
super.measureChildWithMargins(view, 0, 0);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom() + getPaddingBottom() + getDecoratedBottom(view), p.height);
view.measure(childWidthSpec, childHeightSpec);
// Get decorated measurements
measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
}
在Activity中这样使用:
WrapHeightGridLayoutManager gm = new WrapHeightGridLayoutManager(mContext, Constant.RECYCLERVIEW_SIZE_GRIDLAYOUT_FIVE);
mPhotosRcv.setLayoutManager(gm);
这样就完美实现你想要的RecyclerView高度随Item自适应效果了。
表格样式的RecyclerView高度随Item自适应 的解决办法就是自己去重写布局管理器GridLayoutManager。
package com.art.artcircle.widget;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public class WrapHeightGridLayoutManager extends GridLayoutManager {
private int mChildPerLines;
private int[] mMeasuredDimension = new int[2];
public WrapHeightGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
this.mChildPerLines = spanCount;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int height = 0;
for (int i = 0; i < getItemCount(); ) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
height = height + mMeasuredDimension[1];
i = i + mChildPerLines;
}
// If child view is more than screen size, there is no need to make it wrap content. We can use original onMeasure() so we can scroll view.
if (height >heightSize) {
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(widthSize, height);
} else {
super.onMeasure(recycler, state, widthSpec, heightSpec);
}
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
// For adding Item Decor Insets to view
super.measureChildWithMargins(view, 0, 0);
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom() + getPaddingBottom() + getDecoratedBottom(view), p.height);
view.measure(childWidthSpec, childHeightSpec);
// Get decorated measurements
measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
}
在Activity中这样使用:
WrapHeightGridLayoutManager gm = new WrapHeightGridLayoutManager(mContext, Constant.RECYCLERVIEW_SIZE_GRIDLAYOUT_FIVE);
mPhotosRcv.setLayoutManager(gm);
这样就完美实现你想要的RecyclerView高度随Item自适应效果了。
相关文章推荐
- 【linux】Ubuntu下自己编译安装和卸载软件
- hdu 1166
- CDOJ 1134 男神的约会 状压dp
- 基于spark mllib的LDA模型训练源码解析
- Storm 1.0.0 发布
- MATLAB的基础-虽然基础,但全是细节,掌握了这些细节,才是MATLAB高手
- 找到一个数的质因子
- 程序猿之禅
- Java提取两个文件夹中的有差异的文件并生成相应的shell脚本
- android xml之Drawable 篇 --------shape和selector和layer-list的
- 使用Git提交项目到gitHub上
- mysql in
- java CyclicBarrier
- HTML 元素和有效的 DTD
- AngularJS ng-cloak 指令
- Linux基础实践
- 剑指offer-面试题42:翻转单词顺序VS左旋转字符串
- Java获取两个字符串中最大相同子串
- CF 122A. Lucky Division
- Maven搭建Spring+SpringMVC+Mybatis详解,成功搭建me