Android RecycleView嵌套RecycleView 高度不对问题

在使用RecycleView嵌套RecycleView时,可能会发生子RecycleView高度显示不对问题。

注意修改下面几处,在父Adapter的父布局

@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            LayoutInflater.from(context).inflate(R.layout.item_fathe_have_children, null, false);
            return new Holder(view);

        }
    }

被嵌套的RecycleView,即子RecycleView 的Adapter的

@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

            View view = LayoutInflater.from(context).inflate(R.layout.item_child_item, viewGroup, false);
            return new Holder(view);
        }
    }

在父RecycleView 的LinearLayoutManager设置,一定要注意是父RecycleView

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        layoutManager.setAutoMeasureEnabled(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(layoutManager);

同时,注意,子RecycleView在xml文件里的高度设置是wrap_content。

你可能感兴趣的:(android小知识)