RecyclerView跳转到指定位置,RecyclerView上下滑动监听,RecyclerView滑动速度

1、RecyclerView跳转到指定位置

只需调用recycleview的置顶方法:

recyclerView.scrollToPosition(15);

如果你需要让第15item在屏幕居中,只需吧scrollToPosition参数变小即可:

如:

recyclerView.scrollToPosition(12);或

recyclerView.scrollToPosition(9);

即可让15item在屏幕居中

 

注:置顶显示item

mRecyclerView.smoothScrollToPosition(currentIndex);

 

RecyclerView.canScrollVertically(1)的值表示是否能向下滚动,false表示已经滚动到底部
RecyclerView.canScrollVertically(-1)的值表示是否能向上滚动,false表示已经滚动到顶部

 

 

2、RecyclerView上下滑动监听——上拉刷新列表
 

private int lastposion, pagenum = 1, pageContent, num;
gxwbrecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
    //用来标记是否正在向最后一个滑动,既是否向右滑动或向下滑动
    boolean isSlidingToLast = false;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        // 当不滚动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            //获取最后一个完全显示的ItemPosition
            lastposion = manager.findLastCompletelyVisibleItemPosition();
            int totalItemCount = manager.getItemCount();
            if (lastposion == (totalItemCount - 1) && isSlidingToLast&&!ifload) {
                pagenum++;
                if (pagenum>pageContent){
                    Toast.makeText(getContext(), "已到底!", Toast.LENGTH_SHORT).show();
                    return;
                }
                getarealist(1);
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        //dx用来判断横向滑动方向,dy用来判断纵向滑动方向
        //大于0表示,正在向右滚动;小于等于0 表示停止或向左滚动
        isSlidingToLast = dy > 0;
    }
});

 

 

void onScrollStateChanged(RecyclerView recyclerView, int newState): 滚动状态变化时回调
void onScrolled(RecyclerView recyclerView, int dx, int dy): 滚动时回调
/**
 * The RecyclerView is not currently scrolling.(静止没有滚动)
 */
public static final int SCROLL_STATE_IDLE = 0;

/**
 * The RecyclerView is currently being dragged by outside input such as user touch input.
 *(正在被外部拖拽,一般为用户正在用手指滚动)
 */
public static final int SCROLL_STATE_DRAGGING = 1;

/**
 * The RecyclerView is currently animating to a final position while not under outside control.
 *(自动滚动)
 */
public static final int SCROLL_STATE_SETTLING = 2;

 

 

3、安卓手机7.0RecyclerView显示不全解决方法

解决办法是在RecyclerView的外部套上一层RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/menuRv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_16"
            android:layout_marginRight="@dimen/margin_16"/>

</RelativeLayout>

Android ScrollView与RecyclerView滑动冲突问题

解决滑动冲突、滑动不流畅
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

 

滑动速速设置

1、自定义类

public class LinearLayoutManagerWithScrollTop extends LinearLayoutManager {

    public LinearLayoutManagerWithScrollTop(Context context) {
        super(context);
    }

    public LinearLayoutManagerWithScrollTop(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public LinearLayoutManagerWithScrollTop(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        TopSnappedSmoothScroller topSnappedSmoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
        topSnappedSmoothScroller.setTargetPosition(position);
        startSmoothScroll(topSnappedSmoothScroller);
    }

    class TopSnappedSmoothScroller extends LinearSmoothScroller {

        public TopSnappedSmoothScroller(Context context) {
            super(context);
        }

        @Nullable
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return LinearLayoutManagerWithScrollTop.this.computeScrollVectorForPosition(targetPosition);
        }

        /**
         * MILLISECONDS_PER_INCH 默认为25,及移动每英寸需要花费25ms,如果你要速度变快一点,就直接设置设置小一点,注意这里的单位是f
         */
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return 15f / displayMetrics.densityDpi;
        }


        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }
}

2、引用即可

mRecyclerView.setLayoutManager(new LinearLayoutManagerWithScrollTop(this));

 

不可滑动

//        LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
//            public boolean canScrollVertically() {
//                return false;
//            }
//        };
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值