Android 回收器视图不调用任何函数(Kotlin)

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

我有一个简单的新应用程序,带有回收器视图,应该显示“歌曲”,但目前它显示不。 适配器上的任何函数都没有被调用,甚至 getItemCount 也没有被调用。

我尝试过的:

  • 我设置了回收器的背景颜色,我可以看到它出现在屏幕上
  • 我将布局管理器设置为线性,我在 xaml 上尝试了一次,在代码上尝试了一次,但没有解决问题
  • 我已正确设置适配器并调试适配器的构造函数正在被调用,并且它是
  • 已调试以检查项目列表是否为空或 getItemCount 返回 0 而不是
  • 尝试在设置适配器后调用notifyDataSetChanged(),但没有成功

我对 android 并不陌生,我一直在 android xamarin、xamarin maui 上进行开发,现在转移到 android studio。 我似乎找不到我的代码有什么问题。有人可以帮忙吗? 谢谢你。

歌曲适配器:

/**
 * Song adapter
 *
 * @property songs a given list of songs
 * @constructor Create empty Song adapter
 */
class SongAdapter(private val songs: MutableList<Song> = mutableListOf()) : RecyclerView.Adapter<SongAdapter.SongVH>() {

    /**
     * Song view holder
     *
     * @property songDisplayView the song display view being held
     * @constructor Create empty Song view holder
     */
    class SongVH(val songDisplayView: SongDisplayView) : ViewHolder(songDisplayView){
    }

    override fun getItemCount(): Int {
        return songs.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongVH {
        return SongVH(SongDisplayView(parent.context,null,songs[viewType]))
    }

    override fun onBindViewHolder(holder: SongVH, position: Int) {
        holder.songDisplayView.setSong(songs[position])
    }


    /**
     * Update songs to the given new songs
     *
     * @param newSongs given new songs
     */
    fun updateSongs(newSongs: Iterable<Song>){
        songs.clear()
        songs.addAll(newSongs)
        notifyItemRangeChanged(0,songs.size)
    }

    /**
     * Adds a new song to the song list
     *
     * @param newSong a given new song
     */
    fun addSong(newSong : Song){
        songs.add(newSong)
        notifyItemInserted(songs.size-1)
    }

    /**
     * Removes a song from the song list
     *
     * @param index a given index to remove from
     */
    fun removeSong(index: Int){
        songs.removeAt(index)
        notifyItemRemoved(index)
    }

    /**
     * Updates the song at the given index to a new song
     *
     * @param song the new song
     * @param index a given index
     */
    fun updateSong(song: Song, index: Int){
        songs[index] = song
        notifyItemChanged(index)
    }
}

歌曲显示视图:

class SongDisplayView(context: Context,attributeSet: AttributeSet?,song: Song = Song()) : LinearLayout(context,attributeSet) {

    private val titleView: TextView = TextView(context)
    init {
        titleView.text = song.name
        titleView.textSize = 18f
        addView(titleView)
    }

    fun setSong(song: Song ) {
        titleView.text = song.name
    }
}

主要活动:

class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        binding = ActivityMainBinding.inflate(layoutInflater)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val songAdapter = SongAdapter(mutableListOf(Song("Song 1"),Song("Song 2"), Song("Song 3")))
        binding.songsRecycler.adapter = songAdapter
        songAdapter.notifyDataSetChanged()
    }
}

activity_主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<TextView
    android:text="hello world"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    <androidx.recyclerview.widget.RecyclerView
        android:background="#fadada"
        android:id="@+id/songsRecycler"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
android kotlin android-recyclerview android-adapter
1个回答
0
投票

也许这就是问题所在:

setContentView(R.layout.activity_main)
。您应该将 binding.root 设置为内容视图。

© www.soinside.com 2019 - 2024. All rights reserved.