android-fragments 带有协同程序的RecyclerView,使用片段不会填充数据Kotlin

8iwquhpp  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(143)

我正在尝试使用courutines和回收器视图。我已经做了所有必要的事情来发送请求到API,但是我仍然不能得到应该在回收器视图中的列表。我正在使用fragment来创建列表和底部导航。当我转到我的列表应该在的片段时,我得到了错误:我在谷歌上搜索了这个错误,它说我应该在xml中定义布局管理器,但是它已经在我的片段中有了布局管理器

<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list_item"  
    app:layoutManager="LinearLayoutManager"

此外,我的适配器和片段回收器视图看起来像这样:

class MyItemRecyclerViewAdapter(
) : RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder>() {
    
     var userNameResponse = mutableListOf<UsersNameItem>()
    
    fun setNamesList(names: List<UsersNameItem>) {
        this.userNameResponse = userNameResponse.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            FragmentItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = userNameResponse[position]
        holder.idView.text = item.id
        holder.contentView.text = item.name
    }

    override fun getItemCount(): Int = userNameResponse.size

     class ViewHolder(binding: FragmentItemBinding) : RecyclerView.ViewHolder(binding.root) {
        val idView: TextView = binding.itemNumber
        val contentView: TextView = binding.content
    }

}

片段:

class ItemFragment : Fragment() {

    private var layoutManager: RecyclerView.LayoutManager? = null
    private var adapter: RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder>? = null

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

    override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
        super.onViewCreated(itemView, savedInstanceState)
        layoutInflater.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = MyItemRecyclerViewAdapter()
        }
    }

    companion object {
        fun newInstance() = ItemFragment()
    }
}

And MainActivity where I set up courutines and trying to make a request: 

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    lateinit var nameviewModel: ListNamesViewModel
    val adapter = MyItemRecyclerViewAdapter()

    ///trying to create http-request for lists
    val retrofitService = BasicApiService.getInstance()
    val mainRepository = MainRepository(retrofitService)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        nameviewModel = ViewModelProvider(
            this,
            MyViewModelFactory(mainRepository)
        )[ListNamesViewModel::class.java]

        nameviewModel.userName.observe(
            this, Observer {
                adapter.setNamesList(it)
            })
        nameviewModel.message.observe(this) {
            Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
        }

        nameviewModel.loadUsers()

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_dashboard,
                R.id.navigation_notifications,
                R.id.list_name_navig
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }
}

我还想问一下(我对Kotlin很陌生,只是想让事情顺利进行),在MainActivity中将请求留给API是否是一个好的做法,或者我应该用另一种方式来做?

yh2wf1be

yh2wf1be1#

我看到你的代码有些混乱,试着应用下一个:
1.为什么要在ItemFragment中创建layoutManager和适配器?

override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
    super.onViewCreated(itemView, savedInstanceState)
    layoutInflater.apply {
        layoutManager = LinearLayoutManager(activity)
        adapter = MyItemRecyclerViewAdapter()
    }
}

您需要将它和RecyclerView移动到您的Activity中。
1.使用layoutInflater.apply {}没有意义,在您的情况下它没有任何作用。您需要将layoutManager和adapter设置为recyclerView,然后在Activity中它应该是这样的(并将list_item重命名为recyclerView

binding.recyclerView.apply {
    layoutManager = LinearLayoutManager(this)
    adapter = MyItemRecyclerViewAdapter()
}

1.从XML中移除app:layoutManager="LinearLayoutManager"
1.看起来您不需要使用ItemFragment,因为您在ViewHolder中使用它就像使用视图和绑定一样

class ViewHolder(binding: FragmentItemBinding) : RecyclerView.ViewHolder(binding.root) {
    val idView: TextView = binding.itemNumber
    val contentView: TextView = binding.content
}
zqry0prt

zqry0prt2#

尝试在xml中更改

app:layoutManager="LinearLayoutManager"

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

相关问题