简体   繁体   English

Android Kotlin RecyclerView in Fragment 不工作

[英]Android Kotlin RecyclerView in Fragment is not working

I'm very new in Kotlin and Android programming.我是 Kotlin 和 Android 编程的新手。 I tried to create a Fragment which populates a Recycler view, but somehow I get the following error: E/RecyclerView: No adapter attached; skipping layout我试图创建一个填充 Recycler 视图的 Fragment,但不知何故我收到以下错误: E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout I don't really understand why I get this, since I binded everything. E/RecyclerView: No adapter attached; skipping layout我真的不明白为什么会得到这个,因为我绑定了所有内容。 If somebody can explain what I'm doing wrong I would really appreciate it.如果有人可以解释我做错了什么,我将不胜感激。 My code is the following:我的代码如下:

My class:我的 class:

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

My 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) }
        }
    }
}

My adapter我的适配器

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 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

<?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>

You're creating a new view and binding in onViewCreated and setting adapter to it (that view will be garbage collected) you should inflate your view in onCreateView and set the adapter to that recycler view instead of your temporary one.您正在创建一个新视图并在onViewCreated中绑定并为其设置适配器(该视图将被垃圾收集),您应该在onCreateView中膨胀您的视图并将适配器设置为该回收器视图而不是您的临时视图。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM