@[TOC]
1.布局 结构
布局中省略了属性
但是 android:descendantFocusability="blocksDescendants" 这句话 必须加在RecyclerView的父布局上(LinearLayout ,RelativeLayout等都可以)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout >
<androidx.core.widget.NestedScrollView>
<LinearLayout android:descendantFocusability="blocksDescendants">
<androidx.recyclerview.widget.RecyclerView/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
2.自定义Manager(GridLayoutManager ,LinearLayoutManager),设置RecyclerView禁止滑动
public class CustomRecycleViewManager extends GridLayoutManager {
private boolean isScrollEnabled = true;
public CustomRecycleViewManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomRecycleViewManager(Context context, int spanCount) {
super(context, spanCount);
}
public CustomRecycleViewManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
return isScrollEnabled && super.canScrollVertically();
}
}
3. 设置RecyclerView参数
val manager = CustomRecycleViewManager(context, 4);
manager.setScrollEnabled(false)//禁止滑动
rlMyService.isNestedScrollingEnabled = false
rlMyService.setHasFixedSize(true)
rlMyService.layoutManager = manager
rlMyService.adapter = mServiceAdapter
网友评论