android - 我需要在 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) {
}
我尝试了一切,但没有结果。
解决方案
推荐阅读
- isabelle - 使用 `isabelle` 与 jEdit 建立会话
- python - 无法为张量层提供值
- python - /cart/product-1 'QuerySet' 对象的 AttributeError 没有属性 'items'
- visual-studio-code - 授权 Org 命令在 VS Code 中不起作用
- excel - Excel - ExportAsFixedFormat - Pages From & Too 问题
- python - 另一个 Python 线程如何等到锁被释放?
- java - 由于资源限制,JVM 有能力杀死线程吗?
- haskell - 如何在 Haskell 中使用数据结构?
- python - 在 Python 中使用相关矩阵创建具有重复项的集群
- python - 如何使用 Prometheus 监控 Celery 任务完成情况