android ScrollView嵌套RecyclerView

在Android中,将ScrollView嵌套ListView通常通过重写ListView的onMeasure方法,但此方法不适用于RecyclerView。对于RecyclerView,应重写其LayoutManager,如LinearLayoutManager、GridLayoutManager或StaggeredGridLayoutManager。然而,如果RecyclerView内容过长,这种嵌套会导致效率低下,因为所有内容会一次性加载。本文提供了一个实现嵌套的demo,并讨论了在不同Android版本上可能遇到的问题及解决方案,包括在5.x版本上滑动体验不佳的情况,最后提出通过重写ScrollView处理滑动事件的方法。
摘要由CSDN通过智能技术生成

之前ScrollView要嵌套ListView,方法很多,很多人都会使用重写ListView的onMeasure方法来实现,如下:

@Override  
protected void onMeasure(int widthSpec, int heightSpec) {  
    int newHeightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
            MeasureSpec.AT_MOST);  
    super.onMeasure(widthSpec, newHeightSpec);  
}  

但是,RecyclerView使用这个方法却行不通。嵌套RecyclerView,不是要重写RecyclerView,而是要重写LayoutManager,比如LinearLayoutManager、GridLayoutManager和StaggeredGridLayoutManager。但是值得注意的是,如果recyclerView很长那么强烈不建议去做嵌套,因为这样recyclerView会在展示的时候立刻展示所有内容,效率极低。

下面先看一下实现好的demo
这里写图片描述

下面实现LayoutManager的重写方法
1、LinearLayoutManager和ScrollView嵌套

package com.frankzhu.recyclerviewdemo;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


public class FullyLinearLayoutManager extends LinearLayoutManager {
   

    private static final String TAG = FullyLinearLayoutManager.class.getSimpleName();

    public FullyLinearLayoutManager(Context context) {
        super(context);
    }

    public FullyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];

    @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.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        Log.i(TAG, "onMeasure called. \nwidthMode " + widthMode
                + " \nheightMode " + heightSpec
                + " \nwidthSize " + widthSize
                + " \nheightSize " + heightSize
                + " \ngetItemCount() " + getItemCount());

        int width = 0;
        int height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            measureScrapChild(recycler, i,
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                    mMeasuredDimension);

            if (getOrientation() == HORIZONTAL) {
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            } else {
                height = height + mMeasuredDimension[1];
                if (i == 0)
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值