日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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滑动速度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。