RecyclerView,未附加适配器:跳过布局

问题描述 投票:0回答:1

我正在以故事列表的形式创建主页显示。成功登录后,我从 API 检索令牌。令牌已成功检索,但由于错误,列表故事未出现

未连接适配器:跳过布局。


@Suppress("DEPRECATION")
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    companion object{
        const val EXTRA_TOKEN = "extra_token"
    }
    private var binding: ActivityMainBinding? = null
    private var token: String = " "

    private lateinit var recyclerView: RecyclerView
    private lateinit var listAdapter: StoryAdapter
    private val mainViewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        token = intent.getStringExtra(EXTRA_TOKEN).toString()


// Didn't I define the recyclerView first? Why is it still an error?

        listAdapter = StoryAdapter()
        recyclerView = binding?.rvStory!!
        recyclerView.adapter = listAdapter

        lifecycleScope.launchWhenCreated {
            launch {
                mainViewModel.getAuthToken().collect {itToken ->
                    if (!itToken.isNullOrEmpty())
                        token = itToken
                    getAllStories(token)
                    setSwipeLayout()
                }
            }
        }

        binding?.fabAddStory?.setOnClickListener{
            Intent(this, AddStoryActivity::class.java).also { intent ->
                startActivity(intent)
            }
        }

    }

    private fun setSwipeLayout(){
        binding?.srLayout?.setOnRefreshListener {
            getAllStories(token)
            binding!!.srLayout.isRefreshing = false
        }
    }

    private fun getAllStories(token:String){
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mainViewModel.getAllStories(token).collect{ result ->
                    result.onSuccess {
                        val response = result.getOrNull()
                        val liststory = response?.listStory as List<ListStoryItem>
                        Log.d("MainActivity", "List Story Size: ${liststory.size}")

                        setupRecyclerView()
                        setUpadateStories(liststory)
                    }
                    result.onFailure {
                        val response = result.exceptionOrNull()
                        response?.printStackTrace()
                    }
                }
            }
        }
    }

    private fun setUpadateStories(liststory: List<ListStoryItem>){
        val recyclerViewState = recyclerView.layoutManager?.onSaveInstanceState()
        listAdapter.submitList(liststory)
        recyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
    }

    private fun setupRecyclerView() {
        recyclerView = binding?.rvStory!!
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = listAdapter
    }
}

Image of error

kotlin android-recyclerview adapter
1个回答
0
投票

在方法

onCreate
中,您没有附加 LayoutManager。

最简单的解决方案是替换方法中的这些行

onCreate

recyclerView = binding?.rvStory!!
recyclerView.adapter = listAdapter

有了这个:

setupRecyclerView()

您还可以更改方法的定义

setupRecyclerView
以更好地反映您想要的 LayoutManager 的方向:

private fun setupRecyclerView() {
    recyclerView = binding?.rvStory!!
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    recyclerView.adapter = listAdapter
}
© www.soinside.com 2019 - 2024. All rights reserved.