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

Android Fragment中RecyclerView无法正常工作无崩溃问题排查

问题根因及修复方案

1. 核心错误:Fragment视图返回逻辑错误

你在onCreateView方法中先 inflate 得到视图对象v,并在v上完成了RecyclerView的查找和配置,但最后返回时重新 inflate 了一个全新的未做任何配置的布局对象,之前的所有配置完全未生效,这是RecyclerView无内容的最主要原因。

修复代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_ftab2, container, false);
    foundRecycler =(RecyclerView) v.findViewById(R.id.r1foundation);
    foundationRecycler();
    // 不要重新inflate,直接返回已经配置好的v
    return v;
}

2. 嵌套滑动测量异常问题

你使用普通ScrollView嵌套高度为wrap_content的RecyclerView,会出现滑动冲突、测量异常导致的显示不全问题。

修复方案:

  • 把布局文件中的ScrollView替换为androidx.core.widget.NestedScrollView
  • 在RecyclerView初始化代码中添加关闭嵌套滑动的配置:
private void foundationRecycler() {
    // wrap_content高度不要设置setHasFixedSize为true
    foundRecycler.setHasFixedSize(false);
    foundRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
    // 关闭嵌套滑动,适配NestedScrollView
    foundRecycler.setNestedScrollingEnabled(false);
    // 其余原有代码不变
    ArrayList<FoundHelperClass> freeLocatio = new ArrayList<>();
    freeLocatio.add(new FoundHelperClass(R.drawable.secndone, "Colour and Colour Theory - Fundamentals of Visual Design", "In this lesson, goutam explains everything you need to know about colours in UI/UX design. At the end he would also explain about the process of creating a colour palette."));
    adapter1 = new FoundclassesAdapter(freeLocatio);
    foundRecycler.setAdapter(adapter1);
}

3. 导入类错误

你在ftab2.java顶部导入的是FreeHelperClass、FreeclassesAdapter,但实际代码中用的是FoundHelperClass、FoundclassesAdapter,需要修正导入语句,否则会编译报错。

// 删掉错误导入
// import com.athrved.masterclass.uiux.FreeHelperClass;
// import com.athrved.masterclass.uiux.FreeclassesAdapter;
// 替换为实际用到的类
import 你的项目包名.FoundHelperClass;
import 你的项目包名.FoundclassesAdapter;

完成以上三处修改后,RecyclerView即可正常渲染内容。

内容的提问来源于stack exchange,提问作者Prathamesh S Pai

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 00:12:03