RecyclerView滚动到指定位置及高度计算

RecyclerView滚动到指定位置及高度计算

滚动到指定位置

由于RecyclerView的布局复用,看不到的item是暂时没有添加到RecyclerView中的,所以要滚动到看不到的item所在位置时,调用smoothScrollToPosition(int position)并不能滚动到目标位置。
实现思路,每次滚动到最后一个可见item之前,循环滚动,直到滚动到目标位置为止。

/**
 * 优化了 smoothScrollToPosition()
 * Created by zhangjianliang on 2017/11/20
 */
public class MyLinearLayoutManager extends LinearLayoutManager {
   
	/** 目标项是否在最后一个可见项之后 */
    private boolean mShouldScroll;
    /** 记录目标项位置 */
    private int mToPosition;
    private Context mContext;
    private SmoothScrollListener mSmoothScrollListener = new SmoothScrollListener();
	/**
     * 滑动到指定位置
     */
    public void smoothToPosition(RecyclerView recyclerView, final int position) {
   
        if (recyclerView == null || recyclerView.getChildCount() <= 0) {
   
            return;
        }
        recyclerView.removeOnScrollListener(mSmoothScrollListener);
        recyclerView.addOnScrollListener(mSmoothScrollListener);
        smoothMoveToPosition(recyclerView, position);
    }

    private void smoothMoveToPosition(RecyclerView recyclerView, final int position) {
   
        // 第一个可见位置
        int firstItem = recyclerView.getChildLayoutPosition(recyclerView.getChildAt(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值