直播軟體原始碼,自定義RecyclerView支援快速滾動
直播軟體原始碼,自定義RecyclerView支援快速滾動
問題描述:
RecyclerView自帶快速滾動無法控制捲軸的長度,也就是說隨著item的增多,捲軸的長度會越變越小。
解決問題:
透過自定義RecyclerView來實現捲軸的長度不會因為item的增多而發生長度變化。
package com.emsm.app.widget; import android.view.MotionEvent; import androidx.annotation.NonNull; import androidx.core.view.ViewCompat; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import com.emsm.app.util.LogHelps; /** * @Author chentao 0000668668 * @Time 2023/2/10 */ public class ScrollerEvent { private RecyclerView mRecyclerView; private float mInitialBarHeight; private float mLastPressedYAdjustedToInitial; private int mLastAppBarLayoutOffset; public void attachRecyclerView(RecyclerView recyclerView, CallBack call) { this.mRecyclerView = recyclerView; this.mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); LogHelps.i(""); } @Override public void onScrolled(@NonNull RecyclerView parent, int dx, int dy) { super.onScrolled(parent, dx, dy); if (call == null) { return; } // 捲軸拇指的垂直範圍 float extent = parent.computeVerticalScrollExtent(); // 可滾動的區域大小 float range = parent.computeVerticalScrollRange(); // 當前偏移量(當前滾動的距離) float offset = parent.computeVerticalScrollOffset(); // 最大偏移量(最大可滾動的距離) float maxOffset = range - extent; // 可以滑動時,在繪製 if (maxOffset > 0) { // float offsetY = ratio * mMeasureHeight; float ratio = offset / maxOffset; LogHelps.i("dx:" + dx + " dy:" + dy + " extent:" + extent + " range:" + range + " offset:" + offset + " maxOffset:" + maxOffset + " ratio:" + ratio); call.onScrolled(ratio); } } }); } public boolean onTouchEvent(MotionEvent event, int viewHeight) { if (mRecyclerView == null || event == null) { return true; } if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { mRecyclerView.stopScroll(); int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; mRecyclerView.startNestedScroll(nestedScrollAxis); mInitialBarHeight = viewHeight; mLastPressedYAdjustedToInitial = event.getY() + 0; } else if (event.getActionMasked() == MotionEvent.ACTION_MOVE) { float newHandlePressedY = event.getY() + 0; int barHeight = viewHeight; float newHandlePressedYAdjustedToInitial = newHandlePressedY + (mInitialBarHeight - barHeight); float deltaPressedYFromLastAdjustedToInitial = newHandlePressedYAdjustedToInitial - mLastPressedYAdjustedToInitial; int dY = (int) ((deltaPressedYFromLastAdjustedToInitial / mInitialBarHeight) * (mRecyclerView.computeVerticalScrollRange() + 0)); updateRvScroll(dY + mLastAppBarLayoutOffset); mLastPressedYAdjustedToInitial = newHandlePressedYAdjustedToInitial; } else if (event.getActionMasked() == MotionEvent.ACTION_UP) { mLastPressedYAdjustedToInitial = -1; mRecyclerView.stopNestedScroll(); } return true; } public void updateRvScroll(int dY) { if (mRecyclerView == null) { return; } try { mRecyclerView.scrollBy(0, dY); } catch (Exception t) { t.printStackTrace(); } } interface CallBack { // 滾動的比例值 0-1 void onScrolled(float ratio); } /** * 判斷是否可以滾動 * @param recyclerView * @return */ public static boolean isRecyclerScrollable(RecyclerView recyclerView) { if (recyclerView == null) { return false; } float range = recyclerView.computeVerticalScrollRange(); float height = recyclerView.getHeight(); // LogHelps.i("recyclerView的滾動範圍 " + range + " | RecyclerView的高度 " + height); // 滾動範圍大於RecyclerView的高度 說明是可以滾動的 if (true) { return range > height; } boolean h = false; if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); RecyclerView.Adapter adapter = recyclerView.getAdapter(); if (layoutManager == null || adapter == null) { h = false; } else { h = layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1; } } else if (recyclerView.getLayoutManager() instanceof GridLayoutManager) { GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager(); RecyclerView.Adapter adapter = recyclerView.getAdapter(); if (layoutManager == null || adapter == null) { h = false; } else { h = layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1; } } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager(); RecyclerView.Adapter adapter = recyclerView.getAdapter(); if (layoutManager == null || adapter == null) { h = false; } else { h = layoutManager.findLastCompletelyVisibleItemPositions(null)[(layoutManager.getSpanCount() - 1)] < adapter.getItemCount() - 1; } } return h; } }
以上就是 直播軟體原始碼,自定義RecyclerView支援快速滾動,更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2947136/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 直播軟體原始碼,橫向滾動 自定義底部指示器樣式原始碼
- 直播平臺原始碼,迴圈滾動RecyclerView的實現原始碼View
- 直播軟體原始碼,vue 自定義指令過濾特殊字元原始碼Vue字元
- 線上直播系統原始碼,迴圈滾動RecyclerView的實現原始碼View
- 直播軟體原始碼,自定義修改原本已有的圖示原始碼
- 快速運營搭建直播軟體平臺直播原始碼!原始碼
- 基於 RecyclerView 實現的歌詞滾動自定義控制元件View控制元件
- app直播原始碼,java自定義註解APP原始碼Java
- 直播平臺搭建原始碼,qt自定義滑動按鈕原始碼QT
- 直播軟體開發,自動滾動banner
- 直播商城原始碼,vue 彈窗 慣性滾動 加速滾動原始碼Vue
- 直播軟體原始碼,實現頁面滾動到可視區顯示動畫效果原始碼動畫
- RecyclerView滾動位置,滾動速度設定View
- app直播原始碼,自定義兩種Activity切換動畫實現APP原始碼動畫
- 直播系統app原始碼,TabLayout:自定義字型大小APP原始碼TabLayout自定義字型
- 線上直播系統原始碼,自定義底部 BottomNavigationBar原始碼Navigation
- 線上直播原始碼,自定義氣泡效果(BubbleView)原始碼View
- 成品直播原始碼推薦,uniapp多行滾動通知原始碼APP
- 短影片app原始碼,自定義快速捲軸FastScrollBarAPP原始碼AST
- 【Flutter實戰】自定義滾動條Flutter
- JavaScript自定義滾動條詳解JavaScript
- 前端頁面自定義滾動條前端
- app直播原始碼,uniapp之自定義頂部樣式APP原始碼
- 直播平臺原始碼,Flutter 自定義 虛線 分割線原始碼Flutter
- 手機直播原始碼,Flutter 自定義 虛線 分割線原始碼Flutter
- 直播商城原始碼,vue 自定義指令過濾特殊字元原始碼Vue字元
- app直播原始碼,vue 自定義指令過濾特殊字元APP原始碼Vue字元
- 直播系統原始碼,vue實現無縫滾動原始碼Vue
- 直播app原始碼開源,Android 滾動的公告欄APP原始碼Android
- 直播原始碼,懸浮窗滾動漸變色效果原始碼
- 直播軟體搭建,Android實現文字滾動播放效果Android
- 直播系統app原始碼,自定義中間向兩邊滑動的滑動條APP原始碼
- RecyclerView動畫原始碼淺析View動畫原始碼
- 影片直播系統原始碼,自定義背景和狀態管理原始碼
- app直播原始碼,el-button自定義圖片顯示APP原始碼
- 自定義View 之 RecyclerView.ItemDecorationView
- RecyclerView之自定義LayoutManager和SnapHelperView
- 影片直播app原始碼,純css實現橫向滾動APP原始碼CSS