26

Android RecycleView嵌套RecycleView 高度不对问题

 3 years ago
source link: https://blog.csdn.net/aomandeshangxiao/article/details/106299719
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK