Android技术知识Android进阶之路Android

Android RecycleView嵌套RecycleView

2021-02-05  本文已影响0人  Kepler_II

在使用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。

本文在开源项目:https://github.com/Android-Alvin/Android-LearningNotes 中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中...

上一篇下一篇

猜你喜欢

热点阅读