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;@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);?
總結
以上是生活随笔為你收集整理的RecyclerView跳转到指定位置,RecyclerView上下滑动监听,RecyclerView滑动速度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BZOJ2300】【SCOI2011】
- 下一篇: 用计算机改手机电量,三分钟更换iPhon