繁体   English   中英

Android Kotlin RecyclerView in Fragment 不工作

[英]Android Kotlin RecyclerView in Fragment is not working

我是 Kotlin 和 Android 编程的新手。 我试图创建一个填充 Recycler 视图的 Fragment,但不知何故我收到以下错误: E/RecyclerView: No adapter attached; skipping layout E/RecyclerView: No adapter attached; skipping layout我真的不明白为什么会得到这个,因为我绑定了所有内容。 如果有人可以解释我做错了什么,我将不胜感激。 我的代码如下:

我的 class:

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

我的片段:

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>

主要活动.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>

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

分段:

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