Android

NestedScrollView嵌套RecycleView,全部

2020-04-21  本文已影响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内容

上一篇 下一篇

猜你喜欢

热点阅读