下面列出了怎么用android.support.v7.widget.RecyclerView.LayoutParams的API类实例代码及写法,或者点击链接到github查看源代码。
public void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getLeft() - params.leftMargin;
int right = (child.getRight() + params.rightMargin) + this.mDividerHeight;
int top = child.getBottom() + params.bottomMargin;
int bottom = top + this.mDividerHeight;
if (this.mDivider != null) {
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int top = child.getTop() - params.topMargin;
int bottom = child.getBottom() + params.bottomMargin;
int left = child.getRight() + params.rightMargin;
int right = left + this.mDividerHeight;
if (this.mDivider != null) {
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getLeft() - params.leftMargin;
int right = (child.getRight() + params.rightMargin) + this.mDividerHeight;
int top = child.getBottom() + params.bottomMargin;
int bottom = top + this.mDividerHeight;
if (this.mDivider != null) {
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int top = child.getTop() - params.topMargin;
int bottom = child.getBottom() + params.bottomMargin;
int left = child.getRight() + params.rightMargin;
int right = left + this.mDividerHeight;
if (this.mDivider != null) {
this.mDivider.setBounds(left, top, right, bottom);
this.mDivider.draw(c);
}
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
private void recycleFromStart(RecyclerView.Recycler recycler, int line, LayoutManagerHelper helper) {
final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
boolean changed = true;
while (helper.getChildCount() > 0 && changed) {
View child = helper.getChildAt(0);
if (child != null && orientationHelper.getDecoratedEnd(child) < line) {
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int position = lp.getViewPosition();
Span span = findSpan(position, child, true);
if (span != null) {
span.popStart(orientationHelper);
helper.removeChildView(child);
recycler.recycleView(child);
} else {
changed = false;
}
} else {
return;// done
}
}
}
private void recycleFromEnd(RecyclerView.Recycler recycler, int line, LayoutManagerHelper helper) {
final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
final int childCount = helper.getChildCount();
int i;
for (i = childCount - 1; i >= 0; i--) {
View child = helper.getChildAt(i);
if (child != null && orientationHelper.getDecoratedStart(child) > line) {
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int position = lp.getViewPosition();
Span span = findSpan(position, child, false);
if (span != null) {
span.popEnd(orientationHelper);
helper.removeChildView(child);
recycler.recycleView(child);
}
} else {
return;// done
}
}
}
public View nextViewInLimitedList(View ignore) {
int size = this.mScrapList.size();
View closest = null;
int closestDistance = Integer.MAX_VALUE;
for (int i = 0; i < size; i++) {
View view = ((ViewHolder) this.mScrapList.get(i)).itemView;
LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (!(view == ignore || lp.isItemRemoved())) {
int distance = (lp.getViewLayoutPosition() - this.mCurrentPosition) * this.mItemDirection;
if (distance >= 0 && distance < closestDistance) {
closest = view;
closestDistance = distance;
if (distance == 0) {
break;
}
}
}
}
return closest;
}
private View makeAndAddView(int position, Direction direction, Recycler recycler) {
final View child = recycler.getViewForPosition(position);
final boolean isItemRemoved = ((LayoutParams) child.getLayoutParams()).isItemRemoved();
if (!isItemRemoved) {
addView(child, (direction == Direction.END ? -1 : 0));
}
setupChild(child, direction);
if (!isItemRemoved) {
updateLayoutEdgesFromNewChild(child);
}
return child;
}
@Override
protected void layoutChild(View child, Direction direction) {
getLaneForChild(mTempLaneInfo, child, direction);
mLanes.getChildFrame(mChildFrame, getDecoratedMeasuredWidth(child),
getDecoratedMeasuredHeight(child), mTempLaneInfo, direction);
final ItemEntry entry = cacheChildFrame(child, mChildFrame);
layoutDecorated(child, mChildFrame.left, mChildFrame.top, mChildFrame.right,
mChildFrame.bottom);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (!lp.isItemRemoved()) {
pushChildFrame(entry, mChildFrame, mTempLaneInfo.startLane,
getLaneSpanForChild(child), direction);
}
}
/**
* Returns the next item from the scrap list.
* <p>
* Upon finding a valid VH, sets current item position to VH.itemPosition + mItemDirection
*
* @return View if an item in the current position or direction exists if not null.
*/
private View nextViewFromScrapList() {
final int size = mScrapList.size();
for (int i = 0; i < size; i++) {
final View view = mScrapList.get(i).itemView;
final LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (lp.isItemRemoved()) {
continue;
}
if (mCurrentPosition == lp.getViewLayoutPosition()) {
assignPositionFromScrapList(view);
return view;
}
}
return null;
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
this.mDivider.setBounds(child.getLeft() - params.leftMargin, top, (child.getRight() + params.rightMargin) + this.mDivider.getIntrinsicWidth(), top + this.mDivider.getIntrinsicHeight());
this.mDivider.draw(c);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getRight() + params.rightMargin;
int right = left + this.mDivider.getIntrinsicWidth();
this.mDivider.setBounds(left, child.getTop() - params.topMargin, right, child.getBottom() + params.bottomMargin);
this.mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
this.mDivider.setBounds(child.getLeft() - params.leftMargin, top, (child.getRight() + params.rightMargin) + this.mDivider.getIntrinsicWidth(), top + this.mDivider.getIntrinsicHeight());
this.mDivider.draw(c);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
LayoutParams params = (LayoutParams) child.getLayoutParams();
int left = child.getRight() + params.rightMargin;
int right = left + this.mDivider.getIntrinsicWidth();
this.mDivider.setBounds(left, child.getTop() - params.topMargin, right, child.getBottom() + params.bottomMargin);
this.mDivider.draw(c);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(parent.getContext());
int top = child.getBottom() + ((LayoutParams) child.getLayoutParams()).bottomMargin;
int bottom = top + this.mDividerHeight;
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
int top = parent.getPaddingTop();
int bottom = parent.getHeight() - parent.getPaddingBottom();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount - 1; i++) {
View child = parent.getChildAt(i);
int left = child.getRight() + ((LayoutParams) child.getLayoutParams()).rightMargin;
int right = left + this.mDividerHeight;
if (this.mPaint != null) {
c.drawRect((float) left, (float) top, (float) right, (float) bottom, this.mPaint);
}
}
}
void prependToSpan(View view, OrientationHelperEx helper) {
LayoutParams lp = getLayoutParams(view);
mViews.add(0, view);
mCachedStart = INVALID_LINE;
if (mViews.size() == 1) {
mCachedEnd = INVALID_LINE;
}
if (lp.isItemRemoved() || lp.isItemChanged()) {
mDeletedSize += helper.getDecoratedMeasurement(view);
}
}
void appendToSpan(View view, OrientationHelperEx helper) {
LayoutParams lp = getLayoutParams(view);
mViews.add(view);
mCachedEnd = INVALID_LINE;
if (mViews.size() == 1) {
mCachedStart = INVALID_LINE;
}
if (lp.isItemRemoved() || lp.isItemChanged()) {
mDeletedSize += helper.getDecoratedMeasurement(view);
}
}
void popEnd(OrientationHelperEx helper) {
final int size = mViews.size();
View end = mViews.remove(size - 1);
final LayoutParams lp = getLayoutParams(end);
if (lp.isItemRemoved() || lp.isItemChanged()) {
mDeletedSize -= helper.getDecoratedMeasurement(end);
}
if (size == 1) {
mCachedStart = INVALID_LINE;
}
mCachedEnd = INVALID_LINE;
}
void popStart(OrientationHelperEx helper) {
View start = mViews.remove(0);
final LayoutParams lp = getLayoutParams(start);
if (mViews.size() == 0) {
mCachedEnd = INVALID_LINE;
}
if (lp.isItemRemoved() || lp.isItemChanged()) {
mDeletedSize -= helper.getDecoratedMeasurement(start);
}
mCachedStart = INVALID_LINE;
}
public int calculateDyToMakeVisible(View view, int snapPreference) {
LayoutManager layoutManager = getLayoutManager();
if (layoutManager == null || !layoutManager.canScrollVertically()) {
return 0;
}
LayoutParams params = (LayoutParams) view.getLayoutParams();
return calculateDtToFit(layoutManager.getDecoratedTop(view) - params.topMargin, layoutManager.getDecoratedBottom(view) + params.bottomMargin, layoutManager.getPaddingTop(), layoutManager.getHeight() - layoutManager.getPaddingBottom(), snapPreference);
}
public int calculateDxToMakeVisible(View view, int snapPreference) {
LayoutManager layoutManager = getLayoutManager();
if (layoutManager == null || !layoutManager.canScrollHorizontally()) {
return 0;
}
LayoutParams params = (LayoutParams) view.getLayoutParams();
return calculateDtToFit(layoutManager.getDecoratedLeft(view) - params.leftMargin, layoutManager.getDecoratedRight(view) + params.rightMargin, layoutManager.getPaddingLeft(), layoutManager.getWidth() - layoutManager.getPaddingRight(), snapPreference);
}
private View nextViewFromScrapList() {
int size = this.mScrapList.size();
for (int i = 0; i < size; i++) {
View view = ((ViewHolder) this.mScrapList.get(i)).itemView;
LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (!lp.isItemRemoved() && this.mCurrentPosition == lp.getViewLayoutPosition()) {
assignPositionFromScrapList(view);
return view;
}
}
return null;
}
public void assignPositionFromScrapList(View ignore) {
View closest = nextViewInLimitedList(ignore);
if (closest == null) {
this.mCurrentPosition = -1;
} else {
this.mCurrentPosition = ((LayoutParams) closest.getLayoutParams()).getViewLayoutPosition();
}
}
View findReferenceChild(Recycler recycler, State state, int start, int end, int itemCount) {
ensureLayoutState();
View invalidMatch = null;
View outOfBoundsMatch = null;
int boundsStart = this.mOrientationHelper.getStartAfterPadding();
int boundsEnd = this.mOrientationHelper.getEndAfterPadding();
int diff = end > start ? 1 : -1;
for (int i = start; i != end; i += diff) {
View childAt = getChildAt(i);
int position = getPosition(childAt);
if (position >= 0 && position < itemCount) {
if (((LayoutParams) childAt.getLayoutParams()).isItemRemoved()) {
if (invalidMatch == null) {
invalidMatch = childAt;
}
} else if (this.mOrientationHelper.getDecoratedStart(childAt) < boundsEnd && this.mOrientationHelper.getDecoratedEnd(childAt) >= boundsStart) {
return childAt;
} else {
if (outOfBoundsMatch == null) {
outOfBoundsMatch = childAt;
}
}
}
}
if (outOfBoundsMatch == null) {
outOfBoundsMatch = invalidMatch;
}
return outOfBoundsMatch;
}
private BaseViewHolder onCreateLoadHolder(ViewGroup parent) {
mLoadLayout = new LinearLayout(parent.getContext());
mLoadLayout.setOrientation(LinearLayout.VERTICAL);
mLoadLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
if (mParams.loading != null) {
mLoadLayout.addView(mParams.loading);
mParams.loading.setVisibility(View.GONE);
}
if (mParams.loaded != null) {
mLoadLayout.addView(mParams.loaded);
mParams.loaded.setVisibility(View.GONE);
}
return new BaseViewHolder(mLoadLayout);
}
private BaseViewHolder onCreateEmptyHolder(ViewGroup parent) {
mEmptyLayout = (LinearLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_container, parent, false);
if (mParams.empty != null) {
mEmptyLayout.addView(mParams.empty, 0, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
}
return new BaseViewHolder(mEmptyLayout);
}
private BaseViewHolder onCreateFooterHolder(ViewGroup parent) {
mFooterLayout = new LinearLayout(parent.getContext());
mFooterLayout.setOrientation(LinearLayout.VERTICAL);
mFooterLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
for (View footer : mParams.footers) {
mFooterLayout.addView(footer);
}
return new BaseViewHolder(mFooterLayout);
}
private BaseViewHolder onCreateHeaderHolder(ViewGroup parent) {
mHeaderLayout = new LinearLayout(parent.getContext());
mHeaderLayout.setOrientation(LinearLayout.VERTICAL);
mHeaderLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
for (View header : mParams.headers) {
mHeaderLayout.addView(header);
}
return new BaseViewHolder(mHeaderLayout);
}
@Override
public LayoutParams generateDefaultLayoutParams() {
if (mIsVertical) {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
} else {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
}
}