美文网首页Android
NestedScrollView嵌套RecycleView,全部

NestedScrollView嵌套RecycleView,全部

作者: 付小影子 | 来源:发表于2020-04-21 13:59 被阅读0次

    @[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
    

    4.这样就可以在NestedScrollView中全部显示RecycleView的item内容

    相关文章

      网友评论

        本文标题:NestedScrollView嵌套RecycleView,全部

        本文链接:https://www.haomeiwen.com/subject/mveuihtx.html