Android : 片段中的 startActionMode 仅在 onActivityCreated 内部有效
Android : startActionMode in fragment only works inside onActivityCreated
我有一个使用 RecyclerView
的片段。该片段从适配器 class.
实现 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
中工作?
您可能忘记将片段作为 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
我有一个使用 RecyclerView
的片段。该片段从适配器 class.
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
中工作?
您可能忘记将片段作为 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