我不知道如何实现Mediaplayer来为Recyclerview中的每个TITLE播放声音:连续播放声音,播放另一行,停止上一行播放新声音。我希望孩子布局也一样。
我的适配器:
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) {
}
我尝试了所有方法,但没有结果。