简介
使用RecyclerView实现网格布局,实现手机界面应用列表 ,基本逻辑实现,自定义可上下左右滑动的LayoutManager实现,item布局有两种实现方式,
XML布局文件
第一种直接固定一行显示多少,布局文件xml大小固定:
···
按行显示绑定对象;第二种实现每一个单元格的布局绑定:
LayoutManager 实现
创建继承自LinearLayoutManager的FormLayoutManger,并是现实LayoutManager的generateDefaultLayoutParams,确定RecyclerView作用域,
public class FormLayoutManger2 extends LinearLayoutManager {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
}
}
重写onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state),布局item布局展示方式,
// 按第一种实现方式,一行展示
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0 || state.isPreLayout()) {
return;
}
detachAndScrapAttachedViews(recycler);
// 高度宽度统一,获取第一行大小数据,保存到List mItemFrames对象中
View first = recycler.getViewForPosition(0);
measureChildWithMargins(first, 0, 0);
int width = getDecoratedMeasuredWidth(first);
int height = getDecoratedMeasuredHeight(first);
if (mItemFrames == null || mItemFrames.size() != getItemCount()) {
mItemFrames = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
Rect rect = new Rect();
rect.set(0, i * height, width, (i + 1) * height);
mItemFrames.add(rect);
}
}
mTotalWidth = Math.max(width, getHorizontallySpace());
mTotalHeight = Math.max(getItemCount() * height, getVerticalSpace());
// 获取到每一行单元格的宽高,添加到RecycleView中
fill(recycler, state);
}
// 按第二种实现方式,计算每个单元格的宽高,计算展示位置
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0 || state.isPreLayout()) { return;}
detachAndScrapAttachedViews(recycler);
View first = recycler.getViewForPosition(0);
measureChildWithMargins(first, 0, 0);
int width = getDecoratedMeasuredWidth(first);
int height = getDecoratedMeasuredHeight(first);
//定义方向的偏移量
int rows = -1;
mItemFrames = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
int column = i % (spanCount);
View scrap = recycler.getViewForPosition(i);
addView(scrap);
if (column == 0) {
rows++;
}
Rect rect = new Rect();
if (rows == 0) {
rect.set(column * width, 0, (column + 1) * width, height);
} else {
rect.set(column * width, rows * height, (column + 1) * width, (rows + 1) * height);
}
mItemFrames.add(rect);
}
mTotalWidth = Math.max(spanCount * width, getHorizontallySpace());
mTotalHeight = Math.max((rows + 1) * height, getVerticalSpace());
fill(recycler, state);
}
fill构建布局:
private void fill(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0 || state.isPreLayout()) { return;}
detachAndScrapAttachedViews(recycler);
Rect displayRect = new Rect(mSumDx, mSumDy, getHorizontallySpace() + mSumDx, getVerticalSpace() + mSumDy);
for (int i = 0; i < getItemCount(); i++) {
Rect frame = mItemFrames.get(i);
if (Rect.intersects(displayRect, frame)) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
layoutDecorated(scrap, frame.left - mSumDx, frame.top - mSumDy, frame.right - mSumDx, frame.bottom - mSumDy);
}
}
}
布局完成后实现它的滑动事件,实现上下左右滑动,重写canScrollHorizontally(),canScrollVertically(),返回ture,设置可上下左右滑动,然后根据求取的最大宽度和高度,设置滑动的最大高度和宽度,
@Override
public boolean canScrollHorizontally() {
return canHorizontallyScroll;
}
@Override
public boolean canScrollVertically() {
return canVerticallyScroll;
}
public void setCanHorizontallyScroll(boolean canHorizontallyScroll) {
this.canHorizontallyScroll = canHorizontallyScroll;
}
public void setCanVerticallyScroll(boolean canVerticallyScroll) {
this.canVerticallyScroll = canVerticallyScroll;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int travel = dx;
//如果滑动到最顶部
if (mSumDx + dx < 0) {
travel = -mSumDx;
} else if (mSumDx + dx > mTotalWidth - getHorizontallySpace()) {
//如果滑动到最底部
travel = mTotalWidth - getHorizontallySpace() - mSumDx;
}
mSumDx += travel;
// 平移容器内的item
offsetChildrenHorizontal(-travel);
fill(recycler, state);
return dx;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
int travel = dy;
//如果滑动到最顶部
if (mSumDy + dy < 0) {
travel = -mSumDy;
} else if (mSumDy + dy > mTotalHeight - getVerticalSpace()) {
//如果滑动到最底部
travel = mTotalHeight - getVerticalSpace() - mSumDy;
}
mSumDy += travel;
// 平移容器内的item
offsetChildrenVertical(-travel);
fill(recycler, state);
return dy;
}
功能基本就完了 ,完整代码:
public class FormLayoutManger2 extends LinearLayoutManager {
// 同步滑动时间
private final List mRecyclerViews = new ArrayList<>();
// 垂直滑动边界判断
private int mSumDy = 0;
private int mTotalHeight = 0;
// 横向滑动边界判断
private int mSumDx = 0;
private int mTotalWidth = 0;
private boolean canHorizontallyScroll = true;
private boolean canVerticallyScroll = true;
public FormLayoutManger2(Context context, RecyclerView... recyclerViews) {
super(context);
if (recyclerViews != null) {
for (RecyclerView recyclerView : recyclerViews) {
connectRecyclerView(recyclerView);
}
}
}
private SmartRefreshLayout refreshLayout;
public void setRefreshLayout(SmartRefreshLayout refreshLayout) {
this.refreshLayout = refreshLayout;
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0 || state.isPreLayout()) {
return;
}
detachAndScrapAttachedViews(recycler);
View first = recycler.getViewForPosition(0);
measureChildWithMargins(first, 0, 0);
int width = getDecoratedMeasuredWidth(first);
int height = getDecoratedMeasuredHeight(first);
if (mItemFrames == null || mItemFrames.size() != getItemCount()) {
mItemFrames = new ArrayList<>();
for (int i = 0; i < getItemCount(); i++) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
Rect rect = new Rect();
rect.set(0, i * height, width, (i + 1) * height);
mItemFrames.add(rect);
}
}
mTotalWidth = Math.max(width, getHorizontallySpace());
mTotalHeight = Math.max(getItemCount() * height, getVerticalSpace());
fill(recycler, state);
}
private List mItemFrames = new ArrayList<>();
private void fill(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (getItemCount() <= 0 || state.isPreLayout()) {
return;
}
detachAndScrapAttachedViews(recycler);
Rect displayRect = new Rect(mSumDx, mSumDy, getHorizontallySpace() + mSumDx, getVerticalSpace() + mSumDy);
for (int i = 0; i < getItemCount(); i++) {
Rect frame = mItemFrames.get(i);
if (Rect.intersects(displayRect, frame)) {
View scrap = recycler.getViewForPosition(i);
addView(scrap);
measureChildWithMargins(scrap, 0, 0);
layoutDecorated(scrap, frame.left - mSumDx, frame.top - mSumDy, frame.right - mSumDx, frame.bottom - mSumDy);
}
}
}
@Override
public boolean canScrollHorizontally() {
return canHorizontallyScroll;
}
@Override
public boolean canScrollVertically() {
return canVerticallyScroll;
}
public void setCanHorizontallyScroll(boolean canHorizontallyScroll) {
this.canHorizontallyScroll = canHorizontallyScroll;
}
public void setCanVerticallyScroll(boolean canVerticallyScroll) {
this.canVerticallyScroll = canVerticallyScroll;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int travel = dx;
//如果滑动到最顶部
if (mSumDx + dx < 0) {
travel = -mSumDx;
} else if (mSumDx + dx > mTotalWidth - getHorizontallySpace()) {
//如果滑动到最底部
travel = mTotalWidth - getHorizontallySpace() - mSumDx;
}
mSumDx += travel;
// 平移容器内的item
offsetChildrenHorizontal(-travel);
fill(recycler, state);
return dx;
}
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
int travel = dy;
//如果滑动到最顶部
if (mSumDy + dy < 0) {
travel = -mSumDy;
} else if (mSumDy + dy > mTotalHeight - getVerticalSpace()) {
//如果滑动到最底部
travel = mTotalHeight - getVerticalSpace() - mSumDy;
}
mSumDy += travel;
// 平移容器内的item
offsetChildrenVertical(-travel);
fill(recycler, state);
return dy;
}
private int getVerticalSpace() {
return getHeight() - getPaddingBottom() - getPaddingTop();
}
private int getHorizontallySpace() {
return getWidth() - getPaddingEnd() - getPaddingStart();
}
public void connectRecyclerView(RecyclerView recyclerView) {
recyclerView.addOnScrollListener(mScrollListener);
mRecyclerViews.add(recyclerView);
}
private final RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
// 以下场景处理的是,滑动过程中手指松开,移到另外一个recyclerview进行滚动
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
// 该recyclerview进入滚动状态的时候,其余四个要先停止滚动
int currentIndex = mRecyclerViews.indexOf(recyclerView);
for (int i = 0; i < mRecyclerViews.size(); i++) {
if (i != currentIndex) {
mRecyclerViews.get(i).stopScroll();
}
}
}
if (refreshLayout != null) {
if (mSumDy == 0) {
refreshLayout.setEnabled(true);
} else {
refreshLayout.setEnabled(false);
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
// 手指所放在的当前recyclerview的下标
int currentIndex = mRecyclerViews.indexOf(recyclerView);
for (int i = 0; i < mRecyclerViews.size(); i++) {
if (i != currentIndex) {
mRecyclerViews.get(i).scrollBy(dx, dy);
}
}
}
}
};
}