我有一个使用RecyclerView
的片段。该片段从适配器类实现onClickListener
。
在片段中,我想在单击recyclerView项时调用startActionMode。
startActionMode
在onActicityCreated
方法中起作用,但不适用于onClickListener
。
下面是代码:
SongsFrament.kt
package com.akshat.music
import android.content.ContentUris
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.view.*
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_songs.*
import java.io.File
class SongsFragment : Fragment(), SongListAdapter.OnItemClickListener, ActionMode.Callback {
private val allSongs = ArrayList<Songs>()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
recyclerView.apply {
layoutManager = LinearLayoutManager(activity)
adapter = SongListAdapter(allSongs, SongsFragment())
}
activity?.startActionMode(this) // this works
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getAudioDirectories()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater!!.inflate(R.layout.fragment_songs, container, false);
}
override fun onItemClicked(song: Songs, position: Int) {
activity?.startActionMode(this@SongsFragment) // this doesn't work
}
override fun onActionItemClicked(p0: ActionMode?, p1: MenuItem?): Boolean {
return false
}
override fun onCreateActionMode(p0: ActionMode?, p1: Menu?): Boolean {
activity?.menuInflater?.inflate(R.menu.contextual_action_bar, p1)
return true
}
override fun onPrepareActionMode(p0: ActionMode?, p1: Menu?): Boolean {
return true
}
override fun onDestroyActionMode(p0: ActionMode?) {
}
private fun getAudioDirectories() {
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val selection = MediaStore.Audio.Media.IS_MUSIC + "!=0"
val order = MediaStore.Audio.Media.DISPLAY_NAME + " ASC"
val cursor = activity?.contentResolver?.query(uri, null, selection, null, order)
cursor?.let {
it.moveToFirst()
val pathIndex = it.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val path = it.getString(pathIndex)
val file = File(path)
if (!file.exists()) {
continue
}
var songAuth = it.getString(it.getColumnIndex(MediaStore.Audio.Media.ARTIST))
var songName = it.getString(it.getColumnIndex(MediaStore.Audio.Media.TITLE))
val albumID = it.getLong(it.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID))
val sArtworkUri = Uri.parse("content://media/external/audio/albumart")
val albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumID)
var song = Songs(songName, songAuth, albumArtUri)
allSongs.add(song)
} while (it.moveToNext())
}
}
}
如何使startActionMode
在onItemClicked
内部工作?
答案 0 :(得分:0)
您可能忘记了将片段作为OnItemClickListener传递给adpter。 尝试这样的事情:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
recyclerView.apply {
layoutManager = LinearLayoutManager(activity)
adapter = SongListAdapter(allSongs, SongsFragment()).apply {
onItemClickListener = this
}
}
activity?.startActionMode(this) // this works