看完打造屬于你的LayoutManager - huachao1001的專欄 - 博客頻道 - CSDN.NET
編寫的育苟,再次感謝改博主。
public classMyGridLayoutextendsRecyclerView.LayoutManager {
privateActivitymActivity;
private inthorizontallyOffset=0;
private inttotalWidth=0;//控件的總寬度
//保存所有的Item的上下左右的偏移量信息
privateSparseArrayallItemFrames=newSparseArray<>();
//記錄Item是否出現(xiàn)過屏幕且還沒有回收博烂。true表示出現(xiàn)過屏幕上漱竖,并且還沒被回收
privateSparseBooleanArrayhasAttachedItems=newSparseBooleanArray();
publicMyGridLayout(Activity activity) {
this.mActivity= activity;
}
@Override
public voidonLayoutChildren(RecyclerView.Recycler recycler,RecyclerView.State state) {
//如果沒有item馍惹,直接返回
if(getItemCount() <=0)return;
//跳過preLayout,preLayout主要用于支持動畫
if(state.isPreLayout()) {
return;
}
//在布局之前悼吱,將所有的子View先Detach掉良狈,放入到Scrap緩存中
detachAndScrapAttachedViews(recycler);
/**
*定義橫、豎方向的偏移量
*/
intoffsetX =0;
intoffsetY =0;
totalWidth=0;//初始化寬度
intlins =1;//行
intcolumn =0;//頁
intlinNumber =0;
intnumber =0;
DisplayMetrics metric =newDisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
intmaxWidth = metric.widthPixels;//屏幕寬度(像素)
Log.d("[MyGridLayout]","maxWidth:"+ maxWidth);
Log.d("[MyGridLayout]","ItemCount:"+ getItemCount());
for(inti =0;i < getItemCount();i++) {
View scrap = recycler.getViewForPosition(i);//根據(jù)position獲取一個碎片view,可以從回收的view中獲取窥突,也可能新構(gòu)造一個
addView(scrap);
measureChildWithMargins(scrap,0,0);//計算此碎片view包含邊距的尺寸
intwidth = getDecoratedMeasuredWidth(scrap);//獲取此碎片view包含邊距和裝飾的寬度width
intheight = getDecoratedMeasuredHeight(scrap);//獲取此碎片view包含邊距和裝飾的高度height
linNumber = maxWidth / width;
/**
* view復(fù)用
*/
Rect frame =allItemFrames.get(i);
if(frame ==null) {
frame =newRect();
}
if(number >= linNumber) {//第二行
if(number == linNumber) offsetX = offsetX - linNumber * width;
//最后阻问,將View布局到RecyclerView容器中
//? ? ? ? ? ? ? ? layoutDecorated(scrap, offsetX, height, offsetX + width, height * 2);
frame.set(offsetX,height,offsetX + width,height *2);
offsetX += width;
if(number == (linNumber *2-1)) {
number =0;
column++;
}else{
number++;
}
}else{//第一行
//最后,將View布局到RecyclerView容器中
//? ? ? ? ? ? ? ? layoutDecorated(scrap, offsetX, offsetY, offsetX + width, height);
frame.set(offsetX,offsetY,offsetX + width,height);
offsetX += width;
number++;
}
/**
* view復(fù)用
*/
//將當前的Item的Rect邊界數(shù)據(jù)保存
allItemFrames.put(i,frame);
//由于已經(jīng)調(diào)用了detachAndScrapAttachedViews第队,因此需要將當前的Item設(shè)置為未出現(xiàn)過
hasAttachedItems.put(i, false);
//第一行? 第二行
totalWidth= number < linNumber ? offsetX : column * width * linNumber - (maxWidth - linNumber * width);
}
Log.d("[MyGridLayout]","totalWidth-1:"+totalWidth);
//如果所有子View的高度和沒有填滿RecyclerView的高度凳谦,
//則將高度設(shè)置為RecyclerView的高度
totalWidth= Math.max(totalWidth,getHorizontallySpace());
Log.d("[MyGridLayout]","totalWidth-2:"+totalWidth);
/**
* view復(fù)用
*/
recycleAndFillItems(recycler,state);
}
@Override
publicRecyclerView.LayoutParamsgenerateDefaultLayoutParams() {
return newRecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public booleancanScrollHorizontally() {
return true;
}
@Override
public intscrollHorizontallyBy(intdx,RecyclerView.Recycler recycler,RecyclerView.State state) {
//先detach掉所有的子View
detachAndScrapAttachedViews(recycler);
//實際要滑動的距離
inttravel = dx;
Log.d("[MyGridLayout]","travel:"+ travel);
//如果滑動到最頂部
if(horizontallyOffset+ dx <0) {
travel = -horizontallyOffset;
}else if(horizontallyOffset+ dx >totalWidth) {//如果滑動到最底部horizontallyOffset + dx > totalWidth - getHorizontallySpace()
//? ? ? ? ? ? travel = totalWidth - getHorizontallySpace() - horizontallyOffset;
travel =totalWidth-horizontallyOffset;
}
//將橫向的偏移量+travel
horizontallyOffset+= travel;
Log.d("[MyGridLayout]","travel-1:"+ travel);
//平移容器內(nèi)的item
offsetChildrenHorizontal(-travel);
/**
* view復(fù)用
*/
recycleAndFillItems(recycler,state);
returntravel;
}
/**
*回收不需要的Item尸执,并且將需要顯示的Item從緩存中取出
*/
private voidrecycleAndFillItems(RecyclerView.Recycler recycler,RecyclerView.State state) {
if(state.isPreLayout()) {//跳過preLayout如失,preLayout主要用于支持動畫
return;
}
//當前scroll offset狀態(tài)下的顯示區(qū)域
Rect displayFrame =newRect(horizontallyOffset,0,horizontallyOffset+ getHorizontallySpace(),getVerticalSpace());
/**
*將滑出屏幕的Items回收到Recycle緩存中
*/
Rect childFrame =newRect();
for(inti =0;i < getChildCount();i++) {
View child = getChildAt(i);
childFrame.left= getDecoratedLeft(child);
childFrame.top= getDecoratedTop(child);
childFrame.right= getDecoratedRight(child);
childFrame.bottom= getDecoratedBottom(child);
//如果Item沒有在顯示區(qū)域,就說明需要回收
if(!Rect.intersects(displayFrame,childFrame)) {
//回收掉滑出屏幕的View
removeAndRecycleView(child,recycler);
}
}
//重新顯示需要出現(xiàn)在屏幕的子View
for(inti =0;i < getItemCount();i++) {
if(Rect.intersects(displayFrame,allItemFrames.get(i))) {
View scrap = recycler.getViewForPosition(i);
measureChildWithMargins(scrap,0,0);
addView(scrap);
Rect frame =allItemFrames.get(i);// -horizontallyOffset
//將這個item布局出來
layoutDecorated(scrap,
frame.left-horizontallyOffset,
frame.top,
frame.right-horizontallyOffset,
frame.bottom);
}
}
}
/**
*獲取RecyclerView在水平方向上的可用空間,
*即去除了padding后的高度
*/
private intgetHorizontallySpace() {
returngetWidth() - getPaddingLeft() - getPaddingRight();
}
/**
*獲取RecyclerView在豎直方向上的可用空間脆丁,
*即去除了padding后的高度
*/
private intgetVerticalSpace() {
returngetHeight() - getPaddingBottom() - getPaddingTop();
}
}