kotlin snackbar的函数扩展在fragment中不起作用

wvt8vs2t  于 2023-05-01  发布在  Kotlin
关注(0)|答案(2)|浏览(403)

我有这些View函数扩展:

fun View.showSnackbar(msgId: Int, length: Int) {
    showSnackbar(context.getString(msgId), length)
}

fun View.showSnackbar(msg: String, length: Int) {
    showSnackbar(msg, length, null, {})
}

fun View.showSnackbar(
    msgId: Int,
    length: Int,
    actionMessageId: Int,
    action: (View) -> Unit
) {
    showSnackbar(context.getString(msgId), length, context.getString(actionMessageId), action)
}

fun View.showSnackbar(
    msg: String,
    length: Int,
    actionMessage: CharSequence?,
    action: (View) -> Unit
) {
    val snackbar = Snackbar.make(this, msg, length)
    if (actionMessage != null) {
        snackbar.setAction(actionMessage) {
            action(this)
        }.show()
    }
}

我试图在我的片段中使用它,但Snackbar不显示。它没有崩溃,只是没有显示出来。但是,我的showSnackbar2()工作。以下是我在片段中使用它的方式:

class AddressesFragment : Fragment() {
    private lateinit var binding: FragmentAddressesBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentAddressesBinding.inflate(inflater, container, false)
        binding.buttonSnackbar.setOnClickListener {view ->
            //showSnackbar1() // DOES NOT WORK!!
            showSnackbar2()
        }
        return binding.root
    }

    fun showSnackbar1() {
        binding.buttonSnackbar.showSnackbar(
            R.string.location_permission_granted,
            Snackbar.LENGTH_SHORT
        )
    }

    fun showSnackbar2() {
        val view = activity?.findViewById(android.R.id.content) as View
        Snackbar.make(
            view,
            R.string.location_permission_granted,
            Snackbar.LENGTH_SHORT
        ).show()
    }
}

如何修复我的函数扩展,使showSnackbar1()工作?

vc6uscn9

vc6uscn91#

您的Snackbar未显示,因为您正在检查if (actionMessage != null)。如果actionMessage == null,则不会显示Snackbar。请查看下面的详细说明:

fun View.showSnackbar(msgId: Int, length: Int) { // when you called showSnackbar1() this method was called.
    showSnackbar(context.getString(msgId), length)
}

fun View.showSnackbar(msg: String, length: Int) { // Then it goes here with some msg: String and length: Int you passed from the method above
    showSnackbar(msg, length, null, {})
}

fun View.showSnackbar(
    msg: String, // msg from context.getString(msgId)
    length: Int, // Length you passed from your fragment
    actionMessage: CharSequence?, // !!! this param is NULL, because you passed NULL
    action: (View) -> Unit
) {
    val snackbar = Snackbar.make(this, msg, length)
    if (actionMessage != null) { // !!! since actionMessage is null, this method is not being called
        snackbar.setAction(actionMessage) {
            action(this)
        }.show()
    }
}

因此,您需要在最后一个方法中传递action message,以显示您的Snackbar

42fyovps

42fyovps2#

此扩展功能起作用。它扩展View对象并调用Snackbar对象:

fun View.showSnackbar(msg: String,
                      length: Int,
                      actionMessage: CharSequence?,) {
    Snackbar.make(this, msg, length).show()
}

然后按如下方式使用:

val view = this.findViewById(android.R.id.content) as View
view.showSnackbar("Hi", Snackbar.LENGTH_LONG, null)

// Or
binding.buttonSnackbar.showSnackbar("Hi", Snackbar.LENGTH_LONG, null)

相关问题