前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用RecyclerView的几个笔记

使用RecyclerView的几个笔记

作者头像
项勇
发布2019-12-23 17:48:51
6460
发布2019-12-23 17:48:51
举报
文章被收录于专栏:项勇项勇项勇

1.RecyclerView的item宽度设置方法

方法一:重写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布局间距

2.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);
}

3.RecyclerView刷新item的几种方式

刷新全部可见的item,notifyDataSetChanged()
刷新指定item,notifyItemChanged(int)
从指定位置开始刷新指定个item,notifyItemRangeChanged(int,int)
插入、移动一个并自动刷新,notifyItemInserted(int)、notifyItemMoved(int)、notifyItemRemoved(int)
局部刷新,notifyItemChanged(int, Object)

4.RecyclerView实现画廊并渐变大小循环方法

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);
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.RecyclerView的item宽度设置方法
  • 2.RecyclerView跳转到指定item的方法
  • 3.RecyclerView刷新item的几种方式
  • 4.RecyclerView实现画廊并渐变大小循环方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档