OrientationHelper
OrientationHelper是对RecycleView中子View管理的辅助类,它是一个抽象类,类中定义了获取View布局信息的相关方法。虽然是抽象类,但是我们使用中并不需要去继承实现,你会发现createHorizontalHelper和createVerticalHelper两个方法,它是我们今天源码的关键。其实就是在内部new了一个自身的对象,实现了所有的抽象方法,我们直接调用即可。
/**
* Creates a horizontal OrientationHelper for the given LayoutManager.
*
* @param layoutManager The LayoutManager to attach to.
* @return A new OrientationHelper
*/
public static OrientationHelper createHorizontalHelper(
RecyclerView.LayoutManager layoutManager) {
return new OrientationHelper(layoutManager) {
/**
* Returns the end position of the layout after the end padding is removed.
* 返回RecycleView内容右边界位置(RecycleView的宽度-paddingRight)
* @return The end boundary for this layout.
*/
@Override
public int getEndAfterPadding() {
return mLayoutManager.getWidth() - mLayoutManager.getPaddingRight();
}
/**
* Returns the end position of the layout without taking padding into account.
* 返回的就是RecyclerView的width
* @return The end boundary for this layout without considering padding.
*/
@Override
public int getEnd() {
return mLayoutManager.getWidth();
}
/**
* Offset all child views attached to the parent RecyclerView by dx pixels along
* the horizontal axis.
* 对于Horizontal方向的RecyclerView,这个方法的意思就是在水平方向上移动
* RecyclerView 单位是px
* @param dx Pixels to offset by
*/
@Override
public void offsetChildren(int amount) {
mLayoutManager.offsetChildrenHorizontal(amount);
}
/**
* Returns the start position of the layout after the start padding is added.
* 看源码其实就是return mRecyclerView != null ? mRecyclerView.getPaddingLeft() : 0;
* 返回的就是RecyclerView的paddingLeft值
* @return The very first pixel we can draw.
*/
@Override
public int getStartAfterPadding() {
return mLayoutManager.getPaddingLeft();
}
/**
* Returns the space occupied by this View in the current orientation including
* decorations and margins.
* 返回view在水平方向上所占位置的大小 view.width+decoration的左右值+leftMargin+rightMargin
*/
@Override
public int getDecoratedMeasurement(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedMeasuredWidth(view) + params.leftMargin
+ params.rightMargin;
}
/**
* 返回view在竖直方向上所占位置的大小
* view.height+decoration的top bottom值+topMargin+bottomMargin
*/
@Override
public int getDecoratedMeasurementInOther(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedMeasuredHeight(view) + params.topMargin
+ params.bottomMargin;
}
/**
* 返回view右边界点的位置
* view.width+decoration的right值+marginRight,
* 就是子View右边界点到父View的(0,0)点的水平间距
*/
@Override
public int getDecoratedEnd(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedRight(view) + params.rightMargin;
}
/**
* 返回view左边界点的位置
* view.width-decoration的left值-marginLeft,
* 就是子View左边界点到父View的(0,0)点的水平间距
*/
@Override
public int getDecoratedStart(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedLeft(view) - params.leftMargin;
}
/**
* 返回view右侧包含Decoration right值的位置(如果包含decoration的话)
*/
@Override
public int getTransformedEndWithDecoration(View view) {
mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect);
return mTmpRect.right;
}
/**
* 返回view左侧包含Decoration left值的位置(如果包含decoration的话)
*/
@Override
public int getTransformedStartWithDecoration(View view) {
mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect);
return mTmpRect.left;
}
/**
* 返回RecyclerView的宽度-paddingLeft-paddingRight的值
* 刚好是RecyclerView内容区的宽度
*/
@Override
public int getTotalSpace() {
return mLayoutManager.getWidth() - mLayoutManager.getPaddingLeft()
- mLayoutManager.getPaddingRight();
}
/**
* 水平方向上移动view,单位是px
*/
@Override
public void offsetChild(View view, int offset) {
view.offsetLeftAndRight(offset);
}
/**
* 返回mRecyclerView.getPaddingRight()
*/
@Override
public int getEndPadding() {
return mLayoutManager.getPaddingRight();
}
//返回Recycleview宽度测量模式
@Override
public int getMode() {
return mLayoutManager.getWidthMode();
}
//返回Recycleview高度测量模式
@Override
public int getModeInOther() {
return mLayoutManager.getHeightMode();
}
};
}
可以看出,这个类其实就是对LayoutManager的一层封装,用于获取RecyclerView或者其子view的宽高间距等等。另一个方法createVerticalHelper原理和createHorizontalHelper是一样的,可以参考上边的注释进行理解
/**
* Creates a vertical OrientationHelper for the given LayoutManager.
*
* @param layoutManager The LayoutManager to attach to.
* @return A new OrientationHelper
*/
public static OrientationHelper createVerticalHelper(RecyclerView.LayoutManager layoutManager) {
return new OrientationHelper(layoutManager) {
@Override
public int getEndAfterPadding() {
return mLayoutManager.getHeight() - mLayoutManager.getPaddingBottom();
}
@Override
public int getEnd() {
return mLayoutManager.getHeight();
}
@Override
public void offsetChildren(int amount) {
mLayoutManager.offsetChildrenVertical(amount);
}
@Override
public int getStartAfterPadding() {
return mLayoutManager.getPaddingTop();
}
@Override
public int getDecoratedMeasurement(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedMeasuredHeight(view) + params.topMargin
+ params.bottomMargin;
}
@Override
public int getDecoratedMeasurementInOther(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedMeasuredWidth(view) + params.leftMargin
+ params.rightMargin;
}
@Override
public int getDecoratedEnd(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedBottom(view) + params.bottomMargin;
}
@Override
public int getDecoratedStart(View view) {
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
return mLayoutManager.getDecoratedTop(view) - params.topMargin;
}
@Override
public int getTransformedEndWithDecoration(View view) {
mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect);
return mTmpRect.bottom;
}
@Override
public int getTransformedStartWithDecoration(View view) {
mLayoutManager.getTransformedBoundingBox(view, true, mTmpRect);
return mTmpRect.top;
}
@Override
public int getTotalSpace() {
return mLayoutManager.getHeight() - mLayoutManager.getPaddingTop()
- mLayoutManager.getPaddingBottom();
}
@Override
public void offsetChild(View view, int offset) {
view.offsetTopAndBottom(offset);
}
@Override
public int getEndPadding() {
return mLayoutManager.getPaddingBottom();
}
@Override
public int getMode() {
return mLayoutManager.getHeightMode();
}
@Override
public int getModeInOther() {
return mLayoutManager.getWidthMode();
}
};
}