gpt4 book ai didi

Fragment 中的 Android Kotlin RecyclerView 无法正常工作

转载 作者:行者123 更新时间:2023-12-03 08:23:28 26 4
gpt4 key购买 nike

我对 Kotlin 和 Android 编程非常陌生。我尝试创建一个填充 Recycler View 的 fragment ,但不知怎的,我收到以下错误:E/RecyclerView:没有附加适配器;跳过布局我真的不明白为什么我会得到这个,因为我绑定(bind)了一切。如果有人可以解释我做错了什么,我将非常感激。我的代码如下:

我的类(class):

data class Movie(val id:Int, val posterPath:String, val vote:Double, val language:String,val releaseDate:String, val title:String) {}

我的 fragment :

class MovelistScreen : Fragment(R.layout.fragment_movelist_screen) {

@ExperimentalStdlibApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

// View created, can be accessed
// val args = arguments ?: throw IllegalArgumentException("Use new instance method")
// val argValue = args.getString(ARG_NAME)

val binding = FragmentMovelistScreenBinding.inflate(layoutInflater)
val lst : List<Movie> = buildList {
add(Movie(id=1,posterPath="/asdasd",vote=7.3,language="Eng",releaseDate="2017",title="Test1"))
add(Movie(id=2,posterPath="/asdasd",vote=6.3,language="Hun",releaseDate="2013",title="Test2"))
}

val listAdapter = MovieListAdapter()
binding.itemList.adapter=listAdapter
listAdapter.submitList(lst)
}
companion object {
private const val ARG_NAME = "test_argument"
fun newInstance(testArg: String): DetailpageFragment = DetailpageFragment().apply {
arguments = Bundle().apply { putString(ARG_NAME, testArg) }
}
}
}

我的适配器

class MovieListAdapter : ListAdapter<Movie, MovieViewHolder>(diffUtil) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
return MovieViewHolder(MovieItemBinding.inflate(layoutInflater,parent,false))
}

override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val item:Movie=getItem(position)
val binding:MovieItemBinding = holder.binding

binding.movieTitle.text=item.title
binding.releaseYear.text=item.releaseDate
binding.language.text=item.language
binding.ratingtext.text=item.vote.toString()
binding.movieImage.load("https://i.postimg.cc/VLbN4hkz/the-hobbit-the-desolation-of-smaug.jpg")
}
}


private val diffUtil : DiffUtil.ItemCallback<Movie> = object : DiffUtil.ItemCallback<Movie>() {
override fun areItemsTheSame(oldItem: Movie, newItem: Movie): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Movie, newItem: Movie): Boolean = oldItem == newItem
}

class MovieViewHolder(val binding: MovieItemBinding):RecyclerView.ViewHolder(binding.root)

fragment_movelist_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"
android:padding="16dp">
</androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/fragmentmovielist"
android:name="com.example.ubbassignment2.MovelistScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_detailpage" />
</FrameLayout>

最佳答案

您正在创建一个新 View 并在onViewCreated中进行绑定(bind),并为其设置适配器(该 View 将被垃圾收集),您应该在onCreateView中膨胀您的 View 并设置适配器到该回收器 View 而不是您的临时 View 。

fragment :

override fun onCreateView(...): View {
val binding = FragmentMovelistScreenBinding.inflate(layoutInflater)
val lst : List<Movie> = buildList {
add(Movie(id=1,posterPath="/asdasd",vote=7.3,language="Eng",releaseDate="2017",title="Test1"))
add(Movie(id=2,posterPath="/asdasd",vote=6.3,language="Hun",releaseDate="2013",title="Test2"))
}

val listAdapter = MovieListAdapter()
binding.itemList.adapter=listAdapter
listAdapter.submitList(lst)

return binding.root
}

关于Fragment 中的 Android Kotlin RecyclerView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67202064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com