首页 > 解决方案 > 我需要在 recyclerview 中为每个项目实现我自己的 res/raw 文件中的音频

问题描述

我不知道如何实现 mediaplayer 为 Recyclerview 中的每个 TITLE 播放声音:按下一行播放声音,prew 另一行停止前一个并播放新的。我希望子布局也一样。

我的适配器:

class RecAdapter(private val list: List<Movie>?) : RecyclerView.Adapter<RecAdapter.RecViewHolder>() {


    var previousExpandedPosition = -1
        internal set

    internal var mExpandedPosition = -1

    fun getmExpandedPosition(): Int {
        return mExpandedPosition
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecViewHolder {
        val view = LayoutInflater
            .from(parent.context)
            .inflate(R.layout.item_movie, parent, false)
        return RecViewHolder(view)


    }


    override fun onBindViewHolder(holder: RecViewHolder, position: Int) {
        val movie = list!![position]

        holder.bind(movie)

        val isExpanded = position == mExpandedPosition
        holder.subItem.visibility = if (isExpanded) View.VISIBLE else View.GONE


        if (isExpanded)
            previousExpandedPosition = position
        holder.itemView.setOnClickListener {
            mExpandedPosition = if (isExpanded) -1 else position

            notifyDataSetChanged()

        }


    }


    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    inner class RecViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val title: TextView
        private val genre: TextView
        val subItem: View

        init {

            title = itemView.findViewById(R.id.item_title)
            genre = itemView.findViewById(R.id.sub_item_genre)

            subItem = itemView.findViewById(R.id.sub_item)
        }



        fun bind(movie: Movie) {


            title.text = movie.title
            genre.text = "Genre: " + movie.genre

        }
    }

}

主要活动:

class MainActivity : AppCompatActivity() {

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

        val movieList = ArrayList<Movie>()

        movieList.add(Movie("Schindler's List", "Biography, Drama, History"))
        movieList.add(Movie("Pulp Fiction", "Crime, Drama"))
        movieList.add(Movie("No Country for Old Men", "Crime, Drama, Thriller"))
        movieList.add(Movie("Léon: The Professional", "Crime, Drama, Thriller"))
        movieList.add(Movie("Fight Club", "Drama"))
        movieList.add(Movie("Forrest Gump ", "Drama, Romance"))
        movieList.add(Movie("The Shawshank Redemption", "Crime, Drama"))
        movieList.add(Movie("The Godfather", "Crime, Drama"))
        movieList.add(Movie("A Beautiful Mind", "Biography, Drama"))
        movieList.add(Movie("Schindler's List2", "Biography, Drama, History"))
        movieList.add(Movie("Pulp Fiction2", "Crime, Drama"))
        movieList.add(Movie("No Country for Old Men2", "Crime, Drama, Thriller"))
        movieList.add(Movie("Léon: The Professional2", "Crime, Drama, Thriller"))
        movieList.add(Movie("Fight Club2", "Drama"))
        movieList.add(Movie("Forrest Gump2 ", "Drama, Romance"))
        movieList.add(Movie("The Shawshank Redemption2", "Crime, Drama"))
        movieList.add(Movie("The Godfather2", "Crime, Drama"))
        movieList.add(Movie("A Beautiful Mind2", "Biography, Drama"))





        val adapter= RecAdapter(movieList)




        val recyclerView = findViewById<RecyclerView>(R.id.recview)




        recyclerView!!.addItemDecoration(DividerItemDecoration(this, LinearLayoutManager.VERTICAL))






        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        recyclerView.setHasFixedSize(true)
    }
}

和班级:

class Movie(val title: String, val genre: String, val sound: String) {

}

我尝试了一切,但没有结果。

标签: androidandroid-mediaplayer

解决方案


推荐阅读