下面列出了android.support.v7.widget.GridLayoutManager#LayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* orientation为Vertical时调用,处理Vertical下的Offset
* {@link #getItemOffsets(Rect, View, RecyclerView, RecyclerView.State)}
*/
private void handleVertical(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int childPos = parent.getChildAdapterPosition(view);
int sizeAvg = (int) ((mHorizontal * (mSpanCount - 1) + mLeft + mRight) * 1f / mSpanCount);
int spanSize = lp.getSpanSize();
int spanIndex = lp.getSpanIndex();
outRect.left = computeLeft(spanIndex, sizeAvg);
if (spanSize == 0 || spanSize == mSpanCount) {
outRect.right = sizeAvg - outRect.left;
} else {
outRect.right = computeRight(spanIndex + spanSize - 1, sizeAvg);
}
outRect.top = mVertical / 2;
outRect.bottom = mVertical / 2;
if (isFirstRaw(childPos)) {
outRect.top = mTop;
}
if (isLastRaw(childPos)) {
outRect.bottom = mBottom;
}
}
/**
* orientation为Horizontal时调用,处理Horizontal下的Offset
* {@link #getItemOffsets(Rect, View, RecyclerView, RecyclerView.State)}
*/
private void handleHorizontal(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int childPos = parent.getChildAdapterPosition(view);
int spanSize = lp.getSpanSize();
int spanIndex = lp.getSpanIndex();
int sizeAvg = (int) ((mVertical * (mSpanCount - 1) + mTop + mBottom) * 1f / mSpanCount);
outRect.top = computeTop(spanIndex,sizeAvg);
if (spanSize == 0 || spanSize == mSpanCount) {
outRect.bottom = sizeAvg - outRect.top;
} else {
outRect.bottom = computeBottom(spanIndex + spanSize - 1, sizeAvg);
}
outRect.left = mHorizontal/2;
outRect.right = mHorizontal/2;
if (isFirstRaw(childPos)){
outRect.left = mLeft;
}
if (isLastRaw(childPos)){
outRect.right = mRight;
}
}
@Override
public void onBindViewHolder(EditServiceViewHolder holder, int position) {
GridLayoutManager.LayoutParams params = new GridLayoutManager.LayoutParams(mItemSize, mItemSize);
holder.itemView.setLayoutParams(params);
holder.mEditImageView.setImageResource(editBtnResId);
holder.mEditImageView.setVisibility(isEditStatus ? View.VISIBLE : View.INVISIBLE);
Services service = mData.get(position);
ZjbImageLoader.create(service.getIcon())
.setBitmapConfig(Bitmap.Config.ARGB_8888)
.setDisplayType(ZjbImageLoader.DISPLAY_DEFAULT)
.setDefaultDrawable(new ColorDrawable(0xffe0dedc))
.into(holder.mImageView);
holder.itemView.setOnClickListener(v -> {
if (null != mListener && isEditStatus) {
mListener.onItemClick(service, holder.getAdapterPosition());
}
});
}
/**
* Returns whether we consider this a valid view holder for us to draw a divider or section for.
*/
private boolean isValidHolderAndChild(ViewHolder holder, View child,
List<AlphabeticalAppsList.AdapterItem> items) {
// Ensure item is not already removed
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams)
child.getLayoutParams();
if (lp.isItemRemoved()) {
return false;
}
// Ensure we have a valid holder
if (holder == null) {
return false;
}
// Ensure we have a holder position
int pos = holder.getPosition();
if (pos < 0 || pos >= items.size()) {
return false;
}
return true;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
switch (viewType) {
case HEADER:
final View numpadView = inflater.inflate(app.outlay.R.layout.recycler_numpad, parent, false);
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) numpadView.getLayoutParams();
params.height = parent.getMeasuredHeight() - (context.getResources().getDimensionPixelSize(app.outlay.R.dimen.category_item_height) * 2);
final NumpadViewHolder viewHolder = new NumpadViewHolder(numpadView);
return viewHolder;
default:
final View catView = inflater.inflate(app.outlay.R.layout.item_category, parent, false);
final CategoryViewHolder categoryViewHolder = new CategoryViewHolder(catView);
return categoryViewHolder;
}
}
/**
* get span index of given view in row
*
* @param view
* @return
*/
int getSpanIndex(View view) {
if (view.getLayoutParams() instanceof GridLayoutManager.LayoutParams) {
return ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
} else if (view.getLayoutParams() instanceof RecyclerView.LayoutParams) {
return ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
}
return 0;
}
/**
* get span size of given view in row
*
* @param view
* @return
*/
int getSpanSize(View view) {
if (view.getLayoutParams() instanceof GridLayoutManager.LayoutParams) {
return ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanSize();
} else if (view.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
return 1;
}
return 0;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int position = lp.getSpanIndex();
if(position == 0){
outRect.bottom = spacing; // item bottom
return;
}
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
/**
* @return the span index of {@link StaggeredGridLayoutManager.LayoutParams} and {@link GridLayoutManager.LayoutParams},
* otherwise 1 for all other {@link ViewGroup.LayoutParams}
*/
public static <T extends ViewGroup.LayoutParams> int getSpanIndex(@Nullable final T layoutParams) {
if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
return ((StaggeredGridLayoutManager.LayoutParams) layoutParams).getSpanIndex();
} else if (layoutParams instanceof GridLayoutManager.LayoutParams) {
return ((GridLayoutManager.LayoutParams) layoutParams).getSpanIndex();
} else {
return -1;
}
}
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
if (mContext.getResources().getInteger(R.integer.categories_column_count) == 1) {
if (card.getLayoutParams() instanceof GridLayoutManager.LayoutParams) {
GridLayoutManager.LayoutParams params =
(GridLayoutManager.LayoutParams) card.getLayoutParams();
params.leftMargin = 0;
params.rightMargin = 0;
params.topMargin = 0;
params.bottomMargin = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginEnd(0);
}
}
} else {
setCardViewToFlat(card);
}
if (!Preferences.get(mContext).isShadowEnabled()) {
card.setCardElevation(0f);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StateListAnimator stateListAnimator = AnimatorInflater
.loadStateListAnimator(mContext, R.animator.card_lift_long);
card.setStateListAnimator(stateListAnimator);
}
card.setOnClickListener(this);
}
ViewHolder(View itemView, int viewType) {
super(itemView);
if (viewType == TYPE_HEADER) {
name = itemView.findViewById(R.id.name);
holderId = TYPE_HEADER;
} else if (viewType == TYPE_CONTENT) {
icon = itemView.findViewById(R.id.icon);
name = itemView.findViewById(R.id.name);
container = itemView.findViewById(R.id.container);
CardView card = itemView.findViewById(R.id.card);
if (CandyBarApplication.getConfiguration().getApplyGrid() == CandyBarApplication.GridStyle.FLAT) {
if (card.getLayoutParams() instanceof GridLayoutManager.LayoutParams) {
card.setRadius(0f);
card.setUseCompatPadding(false);
int margin = mContext.getResources().getDimensionPixelSize(R.dimen.card_margin);
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) card.getLayoutParams();
params.setMargins(0, 0, margin, margin);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginEnd(margin);
}
}
}
if (!Preferences.get(mContext).isCardShadowEnabled()) {
if (card != null) card.setCardElevation(0);
}
holderId = TYPE_CONTENT;
container.setOnClickListener(this);
}
}
protected int getSpanIndex(RecyclerView parent, View view, RecyclerView.LayoutParams layoutParams) {
if (layoutParams instanceof GridLayoutManager.LayoutParams) {
int currentPosition = parent.getChildAdapterPosition(view);
return ((currentPosition - offsetPosition) % spanCount);
} else if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
return ((StaggeredGridLayoutManager.LayoutParams) layoutParams).getSpanIndex();
}
return 0;
}
@Override
public void onBindViewHolder(ImageHolder holder, int position) {
int side = (getScreenWidth() - getResources().getDimensionPixelOffset(R.dimen.size_5) * 2) / 3;
if(holder.itemView.getLayoutParams() != null){
holder.itemView.getLayoutParams().width = side;
holder.itemView.getLayoutParams().height = side;
}else {
RecyclerView.LayoutParams params = new GridLayoutManager.LayoutParams(side, side);
holder.itemView.setLayoutParams(params);
}
Glide.with(ImagesActivity.this).fromFile().asBitmap().load(new File(imageList.get(position))).into(holder.image);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
int spanCount = gridLayoutManager.getSpanCount();
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int spanIndex = params.getSpanIndex();
int spanSize = params.getSpanSize();
// If it is in column 0 you apply the full offset on the start side, else only half
if (spanIndex == 0) {
outRect.left = padding;
} else {
outRect.left = padding / 2;
}
// If spanIndex + spanSize equals spanCount (it occupies the last column) you apply the full offset on the end, else only half.
if (spanIndex + spanSize == spanCount) {
outRect.right = padding;
} else {
outRect.right = padding / 2;
}
// just add some vertical padding as well
outRect.top = padding / 2;
outRect.bottom = padding / 2;
if(isLayoutRTL(parent)) {
int tmp = outRect.left;
outRect.left = outRect.right;
outRect.right = tmp;
}
}
protected boolean isFirstColumn(GridLayoutManager.LayoutParams params, int pos) {
return params.getSpanIndex() == 0;
}
protected boolean isLastColumn(GridLayoutManager.LayoutParams params, int pos) {
int index = params.getSpanIndex();
int size = params.getSpanSize();
return index + size == mSpanCount;
}
@Override
public void getItemOffsets(Rect outRect, View view, final RecyclerView parent, RecyclerView.State state) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
final int sumCount = state.getItemCount();
final int position = params.getViewLayoutPosition();
final int spanSize;
final int index;
if (params instanceof GridLayoutManager.LayoutParams) {
GridLayoutManager.LayoutParams gridParams = (GridLayoutManager.LayoutParams) params;
spanSize = gridParams.getSpanSize();
index = gridParams.getSpanIndex();
if ((position == 0 || mOldItemCount != sumCount) && mSpanCount > 1) {
int countInLine = 0;
int spanIndex;
for (int tempPosition = sumCount - mSpanCount; tempPosition < sumCount; tempPosition++) {
spanIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanIndex(tempPosition, mSpanCount);
countInLine = spanIndex == 0 ? 1 : countInLine + 1;
}
mItemCountInLastLine = countInLine;
if (mOldItemCount != sumCount) {
mOldItemCount = sumCount;
if (position != 0) {
parent.post(new Runnable() {
@Override
public void run() {
parent.invalidateItemDecorations();
}
});
}
}
}
} else if (params instanceof StaggeredGridLayoutManager.LayoutParams) {
spanSize = ((StaggeredGridLayoutManager.LayoutParams) params).isFullSpan() ? mSpanCount : 1;
index = ((StaggeredGridLayoutManager.LayoutParams) params).getSpanIndex();
} else {
spanSize = 1;
index = 0;
}
if (spanSize < 1 || index < 0 || spanSize > mSpanCount) {
return;
}
outRect.left = mSpace - mRadixX * index;
outRect.right = mRadixX + mRadixX * (index + spanSize - 1);
if (mSpanCount == 1 && position == sumCount - 1) {
outRect.bottom = mSpace;
} else if (position >= sumCount - mItemCountInLastLine && position < sumCount) {
outRect.bottom = mSpace;
}
outRect.top = mSpace;
}
ViewHolder(View itemView) {
super(itemView);
String viewStyle = mContext.getResources().getString(
R.string.wallpaper_grid_preview_style);
Point ratio = ViewHelper.getWallpaperViewRatio(viewStyle);
image = itemView.findViewById(R.id.image);
image.setRatio(ratio.x, ratio.y);
card = itemView.findViewById(R.id.card);
if (CandyBarApplication.getConfiguration().getWallpapersGrid() == CandyBarApplication.GridStyle.FLAT) {
if (card.getLayoutParams() instanceof GridLayoutManager.LayoutParams) {
card.setRadius(0f);
card.setUseCompatPadding(false);
int margin = mContext.getResources().getDimensionPixelSize(R.dimen.card_margin);
GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) card.getLayoutParams();
params.setMargins(0, 0, margin, margin);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginEnd(margin);
}
}
}
if (!Preferences.get(mContext).isCardShadowEnabled()) {
card.setCardElevation(0);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StateListAnimator stateListAnimator = AnimatorInflater
.loadStateListAnimator(mContext, R.animator.card_lift);
card.setStateListAnimator(stateListAnimator);
}
if (mIsShowName) {
name = itemView.findViewById(R.id.name);
author = itemView.findViewById(R.id.author);
}
card.setOnClickListener(this);
card.setOnLongClickListener(this);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
int position = params.getViewPosition();
final int spanSize;
final int index;
if (position > headerCount - 1) {
position -= headerCount;
if (params instanceof GridLayoutManager.LayoutParams) {
GridLayoutManager.LayoutParams gridParams = (GridLayoutManager.LayoutParams) params;
spanSize = gridParams.getSpanSize();
index = gridParams.getSpanIndex();
} else {
spanSize = 1;
index = position % spanCount;
}
// invalid value
if (spanSize < 1 || index < 0) return;
if (spanSize == spanCount) { // full span
outRect.left = space;
outRect.right = space;
} else {
if (index == 0) { // left one
outRect.left = space;
}
// spanCount >= 1
if (index == spanCount - 1) { // right one
outRect.right = space;
}
if (outRect.left == 0) {
outRect.left = space / 2;
}
if (outRect.right == 0) {
outRect.right = space / 2;
}
}
// set top to all in first lane
if (position < spanCount && spanSize <= spanCount) {
if (lastItemInFirstLane < 0) { // lay out at first time
lastItemInFirstLane = position + spanSize == spanCount ? position : lastItemInFirstLane;
outRect.top = space;
} else if (position <= lastItemInFirstLane) { // scroll to first lane again
outRect.top = space;
}
}
outRect.bottom = space;
}
}
@Override
public void onBindViewHolder(GalleryViewHolder holder, int position) {
//1.设置宽高
GridLayoutManager.LayoutParams params = new GridLayoutManager.LayoutParams(mItemSize, mItemSize);
holder.view.setLayoutParams(params);
//2.获取相应的资源进行显示
if (0 == position && openCamera) {
holder.view.setOnClickListener(v -> {
if (mOnRecyclerItemClickListener != null) {
mOnRecyclerItemClickListener.onItemClick(position, null, holder.view);
}
});
} else {
int pos = openCamera ? position - 1 : position;
String url = getUrl(pos);
GalleryPhoto galleryPhoto = mGalleryPhotos.get(pos);
holder.mImageView.setIsSelected(galleryPhoto.isSelected());
ZjbImageLoader.create(url)
.setDisplayType(ZjbImageLoader.DISPLAY_FADE_IN)
.setFadeInTime(800)
.setQiniu(mItemSize, mItemSize)
.setDefaultDrawable(new ColorDrawable(0x00000000))
.into(holder.mImageView);
holder.mImageView.setOnClickListener(v -> {
Integer resourceId = galleryPhoto.getResId();
String uri = "content://media/external/images/media/" + resourceId;
if (galleryPhoto.isSelected()) {
mSelectPhotos.remove(uri);
} else {
if (mSelectPhotos.size() >= 4) {
UIHelper.shortToast(R.string.the_large_num_is_four);
return;
}
mSelectPhotos.add(uri);
}
galleryPhoto.setIsSelected(!galleryPhoto.isSelected());
holder.mImageView.setIsSelected(galleryPhoto.isSelected());
});
}
}