ScrollView里嵌套Recycleview使用StaggeredGridLayoutManager高度不正确的问题

博客介绍了如何解决ScrollView内嵌RecyclerView,当使用StaggeredGridLayoutManager时高度计算不准确的问题。文章补充了在LinearLayoutManager和GridLayoutManager基础上,针对StaggeredGridLayoutManager的解决方案。

http://frank-zhu.github.io/android/2015/02/26/android-recyclerview-part-3/

作者的这个文章中给出了ScrollView中嵌套Recycleview使用LinearLayoutManager和GridLayoutManager重写高度的方法,但是没有StaggeredGridLayoutManager的重写高度。

在此补充,已备后用

public class MyStaggeredGridLayoutManager extends StaggeredGridLayoutManager {


    public MyStaggeredGridLayoutManager(int spanCount, int orientation, Context mContext) {
        super(spanCount, orientation);
        mHeightArray = new int[spanCount];
        this.mContext = mContext;
        for (int i = 0; i < spanCount; i++)
            mHeightArray[i] = 0;
    }

    private int[] mMeasuredDimension = new int[2];
    private int[] mHeightArray;
    private Context mContext;

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        final int widthMode = View.MeasureSpec.getMode(widthSpec);
        final int heightMode = View.Measure
在 Android 开发中,ScrollView 嵌套 RecyclerView 时,常常会遇到滑动冲突或显示全的问题。这主要是由于两者都具备垂直滑动能力,导致事件分发机制混乱,进而影响用户体验。以下是几种有效的解决方案: ### 1. 禁用 RecyclerView 的滑动功能 通过禁用 RecyclerView 的滑动功能,可以避免其与 ScrollView 的滑动冲突。这可以通过设置 `android:nestedScrollingEnabled="false"` 属性来实现,或者在代码中调用 `recyclerView.setNestedScrollingEnabled(false);` [^1]。 此外,也可以通过自定义 `LinearLayoutManager` 来禁止 RecyclerView 的垂直滑动,如下所示: ```java LinearLayoutManager layoutManager = new LinearLayoutManager(this) { @Override public boolean canScrollVertically() { // 直接禁止垂直滑动 return false; } }; recyclerView.setLayoutManager(layoutManager); ``` 这种方法可以有效地将滑动事件交给外层的 ScrollView 处理,从而避免冲突 [^4]。 ### 2. 使用 NestedScrollView 替代 ScrollView 使用 NestedScrollView 替代 ScrollView 是另一种常见的解决方案。NestedScrollView 是 Support 库提供的一个增强型 ScrollView,它支持嵌套滑动,能够更好地与 RecyclerView 配合工作。在 XML 中,可以通过以下方式引用 NestedScrollView: ```xml <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 其他视图 --> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:nestedScrollingEnabled="false" /> </LinearLayout> </androidx.core.widget.NestedScrollView> ``` 通过这种方式,可以确保 RecyclerView 的滑动行为更加流畅,同时避免与外层容器的滑动冲突 [^3]。 ### 3. 自定义测量高度ScrollView 嵌套 RecyclerView 时,RecyclerView 会默认加载所有 item,这会导致复用效率降低。为了避免这种情况,可以通过自定义测量高度的方式来优化性能。具体来说,可以在 RecyclerView 的父容器中测量其高度,并根据实际需求动态调整 RecyclerView高度。这种方法虽然较为复杂,但对于复杂的 UI 布局来说,是值得尝试的 [^2]。 ### 4. 使用头布局 如果上述方法仍然无法满足需求,可以考虑采用头布局的方式。通过将 RecyclerView 放置在一个独立的布局中,并将其作为 ScrollView 的子视图,可以有效地避免滑动冲突。这种方法仅能够提高 UI 的灵活性,还能提升应用的整体性能 [^2]。 ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值