我的适配器程序
class MusicAdapter(private val context: Homefragment, private val musicList: ArrayList<Music>, var listener:OnItemClick? = null, ) : RecyclerView.Adapter<MusicAdapter.Musicholder>() {
class Musicholder(binding: SongviewBinding) : RecyclerView.ViewHolder(binding.root) {
val title = binding.songname
val album = binding.albumname
val songimage = binding.songicon
val songduration = binding.duration
val root = binding.root
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MusicAdapter.Musicholder {
return Musicholder(SongviewBinding.inflate(LayoutInflater.from(parent.context),parent, false))
}
override fun onBindViewHolder(holder: MusicAdapter.Musicholder, position: Int) {
val currentmusic = musicList[position]
holder.title.text = currentmusic.title
holder.album.text = currentmusic.album
holder.songduration.text = formatduration(currentmusic.duration)
Glide.with(context)
.load(musicList[position].artUri)
.placeholder(R.drawable.music_icon_splashscreen).centerCrop()
.into(holder.songimage)
holder.root.setOnClickListener{
listener?.onItemclicked(currentmusic)
}
}
override fun getItemCount(): Int {
return musicList.size
}
interface OnItemClick {
fun onItemclicked(musicList: Music){
}
}
}
现在,我想在recyclerview中获取musiclist的单击位置,并在我的片段中使用它,将其作为Intent传递给另一个Activity。如何获取位置,因为当我尝试获取位置时,它只播放默认位置。
我的片段代码
class Homefragment : Fragment() , MusicAdapter.OnItemClick {
private lateinit var musicAdapter:MusicAdapter
private lateinit var binding: FragmentHomefragmentBinding
companion object{
lateinit var MusicListFA: ArrayList<Music>
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentHomefragmentBinding.inflate(inflater, container, false)
binding.Musicrecycler.setHasFixedSize(true)
binding.Musicrecycler.setItemViewCacheSize(20)
binding.Musicrecycler.layoutManager = LinearLayoutManager(context)
MusicListFA = getAllAudio()
musicAdapter = MusicAdapter(this, MusicListFA, this)
binding.Musicrecycler.adapter = musicAdapter
return binding.root
}
@SuppressLint("Range")
private fun getAllAudio() : ArrayList<Music>{
val tempList = ArrayList<Music>()
val selection = MediaStore.Audio.Media.IS_MUSIC+ "!=0" // to check the file is not null
val projection = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DATE_ADDED,MediaStore.Audio.Media.ALBUM_ID)
val cursor = context?.contentResolver?.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,
MediaStore.Audio.Media.DATE_ADDED + " DESC",null)//to get data from the storage and arrange in descending order
if (cursor != null){
if(cursor.moveToFirst())
do {
val titlec = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
val albumc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM))
val idc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID))
val artistc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
val pathc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
val durationc = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
val albumidc = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)).toString()
val uri = Uri.parse("content://media/external/audio/albumart")
val artUric = Uri.withAppendedPath(uri, albumidc).toString()
val music = Music(title = titlec, album = albumc, artist = artistc, id = idc, path = pathc, duration = durationc, artUri = artUric)
val file = File(music.path)
if(file.exists())
tempList.add(music)
}while (cursor.moveToNext())
cursor.close()
}
return tempList
}
override fun onItemclicked(musicList: Music) {
val intent = Intent(context, Playeractivity::class.java)
intent.putExtra("class", "MusicAdapter")
ContextCompat.startActivity(context!!, intent,null)
}
}
1条答案
按热度按时间olhwl3o21#
和
然后将活动或片段与