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;@Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) {LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();// 当不滚动时if (newState == RecyclerView.SCROLL_STATE_IDLE) {//获取最后一个完全显示的ItemPositionlastposion = 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);}}}@Overridepublic 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);}@Overridepublic 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@Overridepublic PointF computeScrollVectorForPosition(int targetPosition) {return LinearLayoutManagerWithScrollTop.this.computeScrollVectorForPosition(targetPosition);}/*** MILLISECONDS_PER_INCH 默认为25,及移动每英寸需要花费25ms,如果你要速度变快一点,就直接设置设置小一点,注意这里的单位是f*/protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {return 15f / displayMetrics.densityDpi;}@Overrideprotected 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);