方法一:重写RecyclerView.ItemDecoration 在getItemOffsets方法中设置传入的值
/**
* Item spacing for custom RecyclerView
* add by xiangy
*/
public class ItemMarginDecoration extends RecyclerView.ItemDecoration {
int itemMargin = 0;
public ItemMarginDecoration(int itemMargin){
this.itemMargin = itemMargin;
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.left = itemMargin;
outRect.right = itemMargin;
outRect.bottom = itemMargin;
outRect.top = itemMargin;
}
}
Activity中设置
int Margin = 0; //mRecyclerView item Margin
...
mRecyclerView.addItemDecoration(new ItemMarginDecoration(Margin));
方法二:直接设置RecyclerView的item布局间距
a.重写LinearSmoothScroller
public class TopSmoothScroller extends LinearSmoothScroller {
TopSmoothScroller(Context context) {
super(context);
}
@Override
protected int getHorizontalSnapPreference() {
return SNAP_TO_START;//具体见源码注释
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_START;//具体见源码注释
}
}
b.
private LinearLayoutManager Manager;
...
Manager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mManager);
...
/**接收要跳转的位置跳转**/
public void onUpdateItem(int position) {
LinearSmoothScroller s = new TopSmoothScroller(this);
s.setTargetPosition(position);
Manager.startSmoothScroll(s);
}
刷新全部可见的item,notifyDataSetChanged()
刷新指定item,notifyItemChanged(int)
从指定位置开始刷新指定个item,notifyItemRangeChanged(int,int)
插入、移动一个并自动刷新,notifyItemInserted(int)、notifyItemMoved(int)、notifyItemRemoved(int)
局部刷新,notifyItemChanged(int, Object)
public class ZoomRecycleView extends RecyclerView {
private int mScreenWidth;
private int mMinWidth;
private int mMaxWidth;
private float MIN_SCALE = 1f;
private float MAX_SCALE = 1f;
private int mScreenHeight;
private static final int VELOCITY = 2500; //滑动速度
public ZoomRecycleView(@NonNull Context context) {
this(context, null);
}
public ZoomRecycleView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ZoomRecycleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean fling(int velocityX, int velocityY) {
velocityX = calcVelocity(velocityX);
velocityY = calcVelocity(velocityY);
return super.fling(velocityX, velocityY);
}
private int calcVelocity(int velocity) {
if (velocity > 0) {
return Math.min(velocity, VELOCITY);
} else {
return Math.max(velocity, -VELOCITY);
}
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int width = View.MeasureSpec.getSize(widthSpec);
int height = View.MeasureSpec.getSize(heightSpec);
mScreenWidth = width;
mScreenHeight = height;
mMinWidth = (int) (mScreenWidth * 0.28f);
mMaxWidth = mScreenWidth - 2 * mMinWidth;
super.onMeasure(width, height);
}
public void setScale(float min_scale,float max_scale){
MIN_SCALE =min_scale;
MAX_SCALE =max_scale;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
RelativeLayout child = (RelativeLayout) getChildAt(i);
int left = child.getLeft();
int right = mScreenWidth - child.getRight();
final float percent = left < 0 || right < 0 ? 0 : Math.min(left, right) * 1f / Math.max(left, right);//算出一个根据位置计算的等比函数
float scaleFactor = MIN_SCALE + Math.abs(percent) * (MAX_SCALE - MIN_SCALE);
child.setScaleY(scaleFactor);
child.setScaleX(scaleFactor);
if (percent == 1f / 1) { //在中间
((ImageView) child.getChildAt(0)).getDrawable().setLevel(1);
} else {
((ImageView) child.getChildAt(0)).getDrawable().setLevel(0);
}
}
}
}