You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FlexboxLayoutManager的RecyclerView加载item时如何动态调整高度

实现方案

以下是适配FlexboxLayoutManager、支持加载更多且高度动态适配的实现步骤:

1. 修正XML布局参数

你现有布局中包裹NestedScrollView的RelativeLayout高度设为0dp会导致内容无法正常展开,先修改该属性:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"> <!-- 把0dp改为wrap_content -->

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:isScrollContainer="false">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/filter_region_Recycler_lyt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </androidx.recyclerview.widget.RecyclerView>
    </androidx.core.widget.NestedScrollView>
</RelativeLayout>

2. 改造Adapter支持分页显示

给Adapter增加显示状态控制,初始只加载前10条,暴露加载更多的方法:

public class FilterRegionAdapter extends RecyclerView.Adapter<FilterRegionAdapter.ViewHolder> {
    private Context context;
    private List<Region> totalList; // 存储全量30条数据
    private List<Region> showList; // 存储当前要显示的数据
    private boolean isLoadAll = false; // 标记是否已经加载全部数据

    // 构造方法传入全量数据
    public FilterRegionAdapter(Context context, List<Region> totalList) {
        this.context = context;
        this.totalList = totalList;
        this.showList = new ArrayList<>();
        // 初始仅加载前10条
        int initialCount = Math.min(10, totalList.size());
        for (int i = 0; i < initialCount; i++) {
            showList.add(totalList.get(i));
        }
    }

    @Override
    public int getItemCount() {
        return showList.size();
    }

    // 加载更多方法,调用后展示剩余所有数据
    public void loadMoreAll() {
        if (isLoadAll || showList.size() >= totalList.size()) return;
        int startPos = showList.size();
        // 把剩余数据全部加入显示列表
        for (int i = startPos; i < totalList.size(); i++) {
            showList.add(totalList.get(i));
        }
        isLoadAll = true;
        // 局部刷新,比全局刷新性能更好
        notifyItemRangeInserted(startPos, totalList.size() - startPos);
    }

    // 剩下的ViewHolder绑定、onCreateViewHolder等原有逻辑保持不变
    // ...
}

3. 修正Fragment初始化逻辑

删除原有代码中给RecyclerView设置的固定高度,关闭嵌套滚动避免滑动冲突:

filterRegionAdapter = new FilterRegionAdapter(getContext(), regionArrayList);
filterRegionAdapter.setOnFilterRegionItemClickListener(onFilterRegionItemClickListener);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext());
layoutManager.setFlexDirection(FlexDirection.ROW);
layoutManager.setJustifyContent(JustifyContent.FLEX_START);
layoutManager.setAlignItems(AlignItems.CENTER);
// 高度改为wrap_content,不要写死固定值
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
regionRecycler.setLayoutParams(lp);
regionRecycler.setLayoutManager(layoutManager);
regionRecycler.setAdapter(filterRegionAdapter);
// 关闭RecyclerView嵌套滚动,避免和外层NestedScrollView冲突
regionRecycler.setNestedScrollingEnabled(false);

4. 绑定加载更多按钮点击事件

给Show More按钮绑定点击逻辑,点击后触发Adapter加载剩余数据:

TextView loadMoreBtn = findViewById(R.id.filter_region_show_more_text);
loadMoreBtn.setOnClickListener(v -> {
    filterRegionAdapter.loadMoreAll();
    // 加载完成后隐藏按钮,需要收起功能的话可以在这里加切换逻辑
    loadMoreBtn.setVisibility(View.GONE);
});

FlexboxLayoutManager本身已经适配了wrap_content的测量逻辑,每次新增item后会自动重新计算RecyclerView的高度,不需要手动测量适配。

内容的提问来源于stack exchange,提问作者I'm Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 21:06:03