使用RecyclerView实现瀑布流

问题:单独使用RecyclerView实现瀑布流会出现各种问题,如上拉到最顶部出现留白,或者错乱等问题。
解决方案:使用NestedScrollView去嵌套RecyclerView,禁用RecyclerView的滑动,并使用smartRefresh来控制上拉加载和下拉刷新。

步骤:
1、布局
最外层smartRefresh,然后scrollView,最后RecyclerView。记住RecyclerView外层套一层RelativeLayout否则会出现显示不全的情况。

    <com.scwang.smart.refresh.layout.SmartRefreshLayout
        android:id="@+id/srl_index"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
            <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="false"
            android:overScrollMode="never"
            >
                       <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:descendantFocusability="blocksDescendants"
                        >

                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/rv_case_list"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:nestedScrollingEnabled="false"
                            android:overScrollMode="never" />
                    </RelativeLayout>
            </ScrollView>
</com.scwang.smart.refresh.layout.SmartRefreshLayout>

2、activity
其余使用方式和此篇内容是一致的https://www.jianshu.com/p/f9637b10bb8f
也考参考此篇https://www.jianshu.com/p/3a3654a29193

    //绑定案例列表
    fun bindListData() {
        rvCaseList = findViewById<RecyclerView>(R.id.rv_case_list)
        //初始化案例列表
        rvCaseList.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)//GridLayoutManager(me, 2)
        rvCaseList.adapter = indexAdapter
        //点击item跳转到详情页
        indexAdapter.setOnItemClickListener { adapter, view, position ->
//            var id = adapter.getItem(position) as IndexCaseListData.Data
//            var jp = JumpParameter()
//            jp.put("id", id.caseId)
            jump(PreferDetailActivity::class.java)//jp
        }
        //设置模块的子组件的点击事件
        indexAdapter.addChildClickViewIds(R.id.tv_update, R.id.tv_delete)

        //设置子组件事件监听
        indexAdapter.setOnItemChildClickListener { adapter, view, position ->
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容