android-recyclerview - 如何使用 android kotlin 在 recyclerview 适配器中发送空列表?

我正在使用 recyclerview 从实时数据库的数据库中使用 exoplayer 播放视频。当我尝试通过按下后退按钮返回到上一个活动时,视频的音频继续在后台播放。

这是适配器代码:-

class VideoAdapter(
    private var mContext: Context,
    private var mvideos: List<VID>
) : RecyclerView.Adapter<VideoAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.videos_adapter, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val videoid = mvideos[position]

        holder.vidId.text = videoid.getilp()

        val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
        val videoLink = Uri.parse(videoid.getVideoUrl())
        val mediaSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(videoLink))

        holder.mPlayer = SimpleExoPlayer.Builder(mContext).build()
        holder.playerView.player = holder.mPlayer
        holder.mPlayer!!.playWhenReady = true
        holder.mPlayer!!.setMediaSource(mediaSource)
        holder.mPlayer!!.stop()
    }

    override fun getItemCount(): Int {
        return mvideos.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mPlayer: SimpleExoPlayer? = null
        var playerView: PlayerView = itemView.videos_ret
        var vidId: TextView = itemView.videos_id_ret
    }

}

这是我的活动代码:-

class videos : AppCompatActivity() {

    val wimagesRef = FirebaseDatabase.getInstance().getReference().child("videos")

    private var recyclerView: RecyclerView? = null
    private var videoAdapter: VideoAdapter? = null
    private var vid1: MutableList<VID>? = null

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

        recyclerView = videos
        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager = GridLayoutManager(this, 1)

        vid1 = ArrayList()
        videoAdapter = baseContext?.let { VideoAdapter(it, vid1 as ArrayList<VID>) }
        recyclerView?.adapter = videoAdapter

        wimagesRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (snapshot in dataSnapshot.children) {
                    videoAdapter?.notifyDataSetChanged()
                    val video = snapshot.getValue(VID::class.java)
                    if (video != null) {
                        videos.visibility = View.VISIBLE
                        (vid1 as ArrayList<VID>).add(video)
                    }
                }
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
    }

    override fun onBackPressed() {
        super.onBackPressed()

        clearAll()
    }

    fun clearAll() {


        vid1?.clear()
        videoAdapter = baseContext?.let { VideoAdapter(it, vid1) }
        videoAdapter?.notifyDataSetChanged()
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()

        clearAll()
    }
}

我想在适配器中发送空列表以删除适配器中存在的所有数据。有人可以帮帮我吗?

回答1

您可以对 List 使用内置方法 emptyList(),对 ArrayList 使用 arrayListof()

相似文章