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

RecyclerView Adapter无方法执行:Fragment迁移后列表不显示

问题分析与解决方案

核心问题

你的Fragment存在视图绑定实例不匹配的问题:

  • 在onCreateView中手动inflate布局并返回了一个视图实例
  • 但在onViewCreated里又通过PlacnikiFragmentBinding.inflate(layoutInflater)创建了全新的Binding对象,这个Binding对应的视图根本没有被添加到Fragment的视图层级中
  • 你给新Binding里的RecyclerView设置了Adapter,但界面上实际显示的是onCreateView返回的那个未设置Adapter的RecyclerView,所以Adapter的方法完全不会被调用

修正方案(二选一)

方案1:在onCreateView中初始化Binding

class PlacnikiFragment : Fragment() {

    private val TAG = "PlacnikiFragment"
    private lateinit var binding: PlacnikiFragmentBinding
    lateinit var viewModel: MainViewModel
    private val retrofitService = RetrofitService.getInstance()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // 直接通过Binding初始化布局并返回根视图
        binding = PlacnikiFragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 无需重新创建Binding,直接使用已初始化的实例
        viewModel = ViewModelProvider(this, ViewModelFactory(Repository(retrofitService))).get(
            MainViewModel::class.java
        )

        viewModel.placnikiList.observe(viewLifecycleOwner, {
            Log.d(TAG, "placnikiList: $it")
            binding.recyclerview.adapter = PlacnikiAdapter(it)
            // 必须设置LayoutManager(XML中没设置的话,代码里一定要加)
            binding.recyclerview.layoutManager = LinearLayoutManager(requireContext())
        })
        viewModel.errorMessage.observe(viewLifecycleOwner, {
            Log.d(TAG, "errorMessage: $it")
        })
        viewModel.getVsiPlacniki()
    }
}

方案2:通过已有视图绑定Binding

class PlacnikiFragment : Fragment() {

    private val TAG = "PlacnikiFragment"
    private lateinit var binding: PlacnikiFragmentBinding
    lateinit var viewModel: MainViewModel
    private val retrofitService = RetrofitService.getInstance()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.placniki_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 使用Fragment已加载的视图创建Binding,而不是重新inflate
        binding = PlacnikiFragmentBinding.bind(view)
        
        viewModel = ViewModelProvider(this, ViewModelFactory(Repository(retrofitService))).get(
            MainViewModel::class.java
        )

        viewModel.placnikiList.observe(viewLifecycleOwner, {
            Log.d(TAG, "placnikiList: $it")
            binding.recyclerview.adapter = PlacnikiAdapter(it)
            // 必须设置LayoutManager(XML中没设置的话,代码里一定要加)
            binding.recyclerview.layoutManager = LinearLayoutManager(requireContext())
        })
        viewModel.errorMessage.observe(viewLifecycleOwner, {
            Log.d(TAG, "errorMessage: $it")
        })
        viewModel.getVsiPlacniki()
    }
}

额外注意事项

  • 必须设置LayoutManager:RecyclerView如果没有LayoutManager(不管是XML中配置还是代码中设置),无法计算条目布局,即使Adapter设置正确也不会显示内容
  • 可以在Adapter的getItemCount方法中添加日志,验证该方法是否被调用,确认问题是否解决

内容的提问来源于stack exchange,提问作者Jure R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:09:30