在可扩展的recyclerview中实现来自我自己的res / raw的音频文件

时间:2019-05-03 21:52:47

标签: android audio android-recyclerview

我不知道如何从自己的res / raw文件中加载音频文件并将其实现在可扩展的recyclerview列表中 !https://imgur.com/a/ikeaMYqhttps://imgur.com/a/kTwBLAU

我的主要活动:

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",sound1))
    movieList.add(Movie("Pulp Fiction", "Crime, Drama", sound2))
    movieList.add(Movie("No Country for Old Men", "Crime, Drama, Thriller",sound3))
    movieList.add(Movie("Léon: The Professional", "Crime, Drama, Thriller",sound4))
    movieList.add(Movie("Fight Club", "Drama",sound5))
    movieList.add(Movie("Forrest Gump ", "Drama, Romance",sound6))
    movieList.add(Movie("The Shawshank Redemption", "Crime, Drama",sound7))
    movieList.add(Movie("The Godfather", "Crime, Drama",sound8))
    movieList.add(Movie("A Beautiful Mind", "Biography, Drama",sound9))
    movieList.add(Movie("Schindler's List2", "Biography, Drama, History",sound10))
    movieList.add(Movie("Pulp Fiction2", "Crime, Drama",sound11))
    movieList.add(Movie("No Country for Old Men2", "Crime, Drama, Thriller",sound12))
    movieList.add(Movie("Léon: The Professional2", "Crime, Drama, Thriller",sound13))
    movieList.add(Movie("Fight Club2", "Drama",sound14))
    movieList.add(Movie("Forrest Gump2 ", "Drama, Romance",sound15))
    movieList.add(Movie("The Shawshank Redemption2", "Crime, Drama",sound16))
    movieList.add(Movie("The Godfather2", "Crime, Drama",sound17))
    movieList.add(Movie("A Beautiful Mind2", "Biography, Drama",sound18))




    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)
}

}

我的适配器:

RecAdapter类(私有列表:List?):RecyclerView.Adapter(){

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.title.setOnClickListener {
        mExpandedPosition = if (isExpanded) -1 else position
        notifyItemChanged(previousExpandedPosition)( meerge taiat)!!
        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 Movie(val title:String,val genre:String,val sound:String){ }

好..我将标题项展开并单击时将其折叠,但是我希望同时从我自己的res / raw中在背景中加载声音。我尝试了一些与Mediaplayer一起使用的方法,但是没有用。有什么想法吗?

0 个答案:

没有答案