直播软件源码,自定义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
* @Description
* * 专注美妆(香水口红护肤)批发代发-供淘宝/天猫/京东/微商/代购/闲鱼等
* * 主做欧美大牌:迪奥/阿玛尼/祖马龙/香奈儿/古驰/TF/MAC/圣罗兰等等
* * 只做高品质产品!(送朋友亲人客户公司活动以及自用或泡妞等等)
* * +V:em-smart-99999
*/
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支持快速滚动,更多内容欢迎关注之后的文章
标签:自定义,int,RecyclerView,float,源码,layoutManager,null,recyclerView From: https://www.cnblogs.com/yunbaomengnan/p/17336619.html