package com.dongnao.layoutmanagerflow;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
//自定义流式布局管理器
//这个Demo主要是为了演示自定义LayoutManager
//很多代码都做了简化处理,在性能上并不够好
public class FlowLayoutManager extends RecyclerView.LayoutManager {
//保存所有item偏移量信息,所有数据高度和
int totalHeight = 0;
SparseArray<Rect> itemBounds = new SparseArray<>();
//滑动偏移量
int verticalScrollOffset = 0;
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
//摆放
if (getItemCount() <= 0)
return;
//布局动画
if (state.isPreLayout())
return;
//将视图分离放入scrap缓存中,以准备重新对view进行排版
detachAndScrapAttachedViews(recycler);
int offsetY = 0;
int offsetX = 0;
int itemHeight = 0;
for (int i = 0; i < getItemCount(); i++) {
View view = recycler.getViewForPosition(i);
addView(view);
measureChildWithMargins(view, 0, 0);
int w = getDecoratedMeasuredWidth(view);
int h = getDecoratedMeasuredHeight(view);
itemHeight = h;
Rect bound = itemBounds.get(i);
if (bound == null)
bound = new Rect();
//需要换行
if (offsetX + w > getWidth()) {
//换行的View的值
offsetY += h;
offsetX = w;
bound.set(0, offsetY, w, offsetY + h);
} else {
//不需要换行
bound.set(offsetX, offsetY, offsetX + w, offsetY + h);
offsetX += w;
}
//计算前View的布局区域,然后放到allItemframs数组
itemBounds.put(i, bound);
}
totalHeight = offsetY + itemHeight;
//回收不可见的元素
recyclerViewFillView(recycler, state);
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
detachAndScrapAttachedViews(recycler);
//实际滑动距离
int trval = dy;
if (verticalScrollOffset + dy < 0) {
trval = -verticalScrollOffset;
} else if (verticalScrollOffset + dy > totalHeight - getHeight()) {
trval = totalHeight - getHeight() - verticalScrollOffset;
}
//边界值判断
verticalScrollOffset += trval;
//平移容器内的item
offsetChildrenVertical(trval);
recyclerViewFillView(recycler, state);
return trval;
}
@Override
public boolean canScrollVertically() {
return true;
}
protected void recyclerViewFillView(RecyclerView.Recycler recycler, RecyclerView.State state) {
//清空RecyclerView的子View
detachAndScrapAttachedViews(recycler);
Rect phoneFrame = new Rect(0, verticalScrollOffset, getWidth(), verticalScrollOffset + getHeight());
//将滑出屏幕的view进行回收
Rect childRect = new Rect();
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
Rect child = itemBounds.get(i);
if (!Rect.intersects(phoneFrame, child))
removeAndRecycleView(childView, recycler);
}
//可见区域出现在屏幕上的子view
for (int j = 0; j < getItemCount(); j++)
if (Rect.intersects(phoneFrame, itemBounds.get(j))) {
//scrap回收池里面拿的
View scrap = recycler.getViewForPosition(j);
measureChildWithMargins(scrap, 0, 0);
addView(scrap);
Rect frame = itemBounds.get(j);
layoutDecorated(scrap, frame.left, frame.top - verticalScrollOffset, frame.right, frame.bottom - verticalScrollOffset);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android自定义FlowLayoutManager
共32个文件
xml:10个
png:10个
gradle:3个
需积分: 5 0 下载量 6 浏览量
2022-04-06
12:58:42
上传
评论
收藏 129KB ZIP 举报
温馨提示
Android自定义FlowLayoutManager
资源详情
资源评论
资源推荐
收起资源包目录
自定义FlowLayoutManager.zip (32个子文件)
demo-ui
gradlew.bat 2KB
gradlew 5KB
gradle.properties 89B
app
src
main
res
mipmap-xxxhdpi
ic_launcher.png 9KB
ic_launcher_round.png 15KB
drawable
ic_launcher_background.xml 6KB
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 214B
strings.xml 83B
styles.xml 394B
mipmap-xxhdpi
ic_launcher.png 6KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 4KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher_round.xml 276B
ic_launcher.xml 276B
layout
item_view_pager.xml 476B
activity_main.xml 510B
java
com
dongnao
layoutmanagerflow
FlowLayoutManager.java 4KB
Adapter.java 1KB
MainActivity.java 698B
AndroidManifest.xml 743B
build.gradle 917B
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 205B
local.properties 67B
settings.gradle 13B
build.gradle 1KB
共 32 条
- 1
命运之手
- 粉丝: 461
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0