本文主要是介绍RecyclerView瀑布流优化方案探讨,Android屏幕适配很难嘛其实也就那么回事,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
int itemCount = adapter.getItemCount();
int childCount = layoutManager.getChildCount();
RefreshLogUtils.d(“SpaceViewItemLine–count–”+itemCount + “-----”+childCount+"—索引–"+position+"—"+spanIndex);
if (position<itemCount && spanCount2) {
if (childCount % 2 == 0){
//这个是右边item
outRect.left = 5;
outRect.right = 20;
} else {
//这个是左边item
outRect.left = 20;
outRect.right = 5;
}
if (childCount1 || childCount==2){
outRect.top = 0;
} else {
outRect.top = 20;
}
RefreshLogUtils.d(“SpaceViewItemLine–间距–”+childCount+"----"+outRect.left+"-----"+outRect.right);
}
}
});
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
@NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int position = parent.getChildAdapterPosition(view);
int spanCount = 0;
int spanIndex = 0;
RecyclerView.Adapter adapter = parent.getAdapter();
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (adapternull || layoutManagernull){
return;
}
if (layoutManager instanceof StaggeredGridLayoutManager){
spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
}
//普通Item的尺寸
int itemCount = adapter.getItemCount();
int childCount = layoutManager.getChildCount();
RefreshLogUtils.d(“SpaceViewItemLine–count–”+itemCount + “-----”+childCount+"—索引–"+position+"—"+spanIndex);
if (position<itemCount && spanCount2) {
if (spanIndex != GridLayoutManager.LayoutParams.INVALID_SPAN_ID) {
//getSpanIndex方法不管控件高度如何,始终都是左右左右返回index
if (spanIndex % 2 == 0) {
//这个是左边item
outRect.left = 20;
outRect.right = 5;
} else {
//这个是右边item
outRect.left = 5;
outRect.right = 20;
}
if (childCount1 || childCount==2){
outRect.top = 0;
} else {
outRect.top = 20;
}
}
//outRect.top = space;
RefreshLogUtils.d(“SpaceViewItemLine–间距–”+spanIndex+"----"+outRect.left+"-----"+outRect.right);
}
}
});
05.自定义Manager崩溃
-
RecyclerView莫名的Inconsistency detected崩溃;
-
出现这个异常原因:
-
使用RecyclerView加官方下拉刷新的时候,如果绑定的List对象在更新数据之前进行了clear,而这时用户紧接着迅速上滑RV,就会造成崩溃,而且异常不会报到你的代码上,属于RV内部错误。
-
自定义一个CustomStaggeredGridLayoutManager 在onLayoutChildren对异常进行捕获:
public class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager {
private static final String TAG = “LOG_CustomStaggered”;
public CustomStaggeredGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
这篇关于RecyclerView瀑布流优化方案探讨,Android屏幕适配很难嘛其实也就那么回事的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!