为什么我的 imageView 不会随 registerForActivityResult 而更改?



我想制作一个应用程序,点击bottomNavigationView上的button,打开PopupWindow,我可以从我的图库中选择图像(缩略图(和视频,添加描述和标题。然后将提供的数据添加到recyclerView。但我在从图库中拾取图像时遇到了问题,它似乎有效,我可以从图库中提取图像,但它不会像以前那样改变popupWindow中的图像。

主要活动.kt

class MainActivity : AppCompatActivity() {
private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()

private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
val dialogView: View = layoutInflater.inflate(R.layout.popup, null)
dialogView.imageChange.setImageURI(it)
})
private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
val dialogView: View = layoutInflater.inflate(R.layout.popup, null)
dialogView.VideoSelect.setImageURI(it)
})
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val popupButton: FloatingActionButton = findViewById(R.id.fab)
val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)
bottomNav.background = null
bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
replaceFragment(home)
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> replaceFragment(home)
R.id.player -> replaceFragment(player)
R.id.profile -> replaceFragment(profile)
R.id.settings -> replaceFragment(settings)
}
true
}
popupButton.setOnClickListener {
showDialog()
}
}
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
private fun showDialog() {
val title = RecyclerAdapter().titles
val description = RecyclerAdapter().details
val video = RecyclerAdapter().videos
val dialog = Dialog(this)
val dialogView: View = layoutInflater.inflate(R.layout.popup, null)
val imageView = dialogView.findViewById<ImageView>(R.id.imageChange)
val videoView = dialogView.findViewById<ImageView>(R.id.VideoSelect)
val buttonImage = dialogView.findViewById<Button>(R.id.addImage)
val titleEditText = dialogView.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
val descEditText = dialogView.findViewById<EditText>(R.id.description) //popUp pole edit field description

dialogView.addImage.setOnClickListener {
getPreviewImage.launch("image/*")
}
dialogView.addVideo.setOnClickListener {
getPreviewVideo.launch("video/*")
}
dialogView.addButton.setOnClickListener {
if (titleEditText.text.isEmpty()){
Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
dialog.setContentView(dialogView)
dialog.show()
}
}

尝试为dialogView布局使用全局变量,并检查URI是否存在:

class MainActivity : AppCompatActivity() {
private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()
private var dialogView: View? = null

private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
it?.let { uri -> 
dialogView?.findViewById<ImageView>(R.id.imageChange)?.setImageURI(it)
} ?: run {
Log.e("MainActivity", "URI not present")
}
})

private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
it?.let { uri -> 
dialogView?.findViewById<ImageView>(R.id.VideoSelect)?.setImageURI(it)
} ?: run {
Log.e("MainActivity", "URI not present")
}
})

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val popupButton: FloatingActionButton = findViewById(R.id.fab)
val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)

bottomNav.background = null
bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
replaceFragment(home)

bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> replaceFragment(home)
R.id.player -> replaceFragment(player)
R.id.profile -> replaceFragment(profile)
R.id.settings -> replaceFragment(settings)
}
true
}
popupButton.setOnClickListener {
showDialog()
}

}
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}

private fun showDialog() {

val title = RecyclerAdapter().titles
val description = RecyclerAdapter().details
val video = RecyclerAdapter().videos

val dialog = Dialog(this)
dialogView = layoutInflater.inflate(R.layout.popup, null)

val buttonImage = dialogView?.findViewById<Button>(R.id.addImage)

val titleEditText = dialogView?.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
val descEditText = dialogView?.findViewById<EditText>(R.id.description) //popUp pole edit field description

dialogView?.addImage.setOnClickListener {
getPreviewImage.launch("image/*")
}

dialogView?.addVideo.setOnClickListener {
getPreviewVideo.launch("video/*")
}

dialogView?.addButton.setOnClickListener {
if (titleEditText.text.isEmpty()){
Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
dialog.setContentView(dialogView)
dialog.show()
}
}
popupButton.setOnClickListener {
showDialog()
// Should it be notified here that the fragment updates the ui data
}

这是最后一段代码,多亏了@Luca Pizzini。但是有一些变化

class MainActivity : AppCompatActivity() {
private val home = HomeFragment()
private val player = PlayerFragment()
private val profile = ProfileFragment()
private val settings = SettingsFragment()
private var dialogView: View? = null

private val getPreviewImage = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
it?.let { uri ->
dialogView?.findViewById<ImageView>(R.id.imageChange)?.setImageURI(it)
}?:run {
Log.e("MainActivity", "URI not present")
}
})
private val getPreviewVideo = registerForActivityResult(ActivityResultContracts.GetContent(), ActivityResultCallback {
it?.let { uri ->
dialogView?.findViewById<ImageView>(R.id.VideoSelect)?.setImageURI(it)
}?: run{
Log.e("MainActivity", "URI not present")
}
})
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val popupButton: FloatingActionButton = findViewById(R.id.fab)
val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)
bottomNav.background = null
bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
replaceFragment(home)
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> replaceFragment(home)
R.id.player -> replaceFragment(player)
R.id.profile -> replaceFragment(profile)
R.id.settings -> replaceFragment(settings)
}
true
}
popupButton.setOnClickListener {
showDialog()
}
}
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
private fun showDialog() {
val title = RecyclerAdapter().titles
val description = RecyclerAdapter().details
val video = RecyclerAdapter().videos
val dialog = Dialog(this)
dialogView = layoutInflater.inflate(R.layout.popup, null)

val buttonImage = dialogView?.findViewById<Button>(R.id.addImage)
val titleEditText = dialogView?.findViewById<EditText>(R.id.titleEdit) //popUp edit field title
val descEditText = dialogView?.findViewById<EditText>(R.id.description) //popUp pole edit field description

dialogView?.addImage?.setOnClickListener {
getPreviewImage.launch("image/*")
}
dialogView?.addVideo?.setOnClickListener {
getPreviewVideo.launch("video/*")
}
dialogView?.addButton?.setOnClickListener {
if (titleEditText?.text?.isEmpty() == true){
Toast.makeText(applicationContext, "add required data", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
dialog.setContentView(dialogView!!)
dialog.show()
}
}

相关内容

  • 没有找到相关文章

最新更新