`SpannableStringBuilder`无法为来自Firebase的每个数据显示图像跨度(Kotlin)

huangapple go评论68阅读模式
英文:

SpannableStringBuilder not display image span for every data from Firebase (Kotlin)

问题

这是输出:

`SpannableStringBuilder`无法为来自Firebase的每个数据显示图像跨度(Kotlin)

我希望每个数据都有一张图片。

这是我的代码:

val database = FirebaseDatabase.getInstance().reference

val getData = object : ValueEventListener {
     override fun onDataChange(snapshot: DataSnapshot) {
     
          val myData = SpannableStringBuilder()

          for (i in snapshot.children) {
               val data = i.child("myData").value

               myData.append("$data\n\n")
               
               // 从drawable获取图像并设置大小
               val drawable: Drawable? = context?.let { ContextCompat.getDrawable(it, R.drawable.myImage) }
               drawable?.setBounds(0, 0, textView.lineHeight, textView.lineHeight)
               
               // 将图像span设置为第一个字母
               val span: ImageSpan? = drawable?.let { ImageSpan(it, ImageSpan.ALIGN_BASELINE) }
               myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
          }

          textView.text = myData
     }

     override fun onCancelled(error: DatabaseError) {

     }
}

database.addValueEventListener(getData)
英文:

I retrieve the data from FireBase and trying to display an image next to each data (TextView). But the image is displayed on the first data only.

This is the output:

`SpannableStringBuilder`无法为来自Firebase的每个数据显示图像跨度(Kotlin)

I want every data to have an image.

Here is my code:

val database = FirebaseDatabase.getInstance().reference

val getData = object : ValueEventListener {
     override fun onDataChange(snapshot: DataSnapshot) {
     
          val myData = SpannableStringBuilder()

          for (i in snapshot.children) {
               val data = i.child("myData").value

               myData.append("$data\n\n")
               
               // Get image from drawable and set size
               val drawable: Drawable? = context?.let { ContextCompat.getDrawable(it, R.drawable.myImage) }
               drawable?.setBounds(0, 0, textView.lineHeight, textView.lineHeight)
               
               // Set image span to the first letter
               val span: ImageSpan? = drawable?.let { ImageSpan(it, ImageSpan.ALIGN_BASELINE) }
               myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
          }

          textView.text = myData
     }

     override fun onCancelled(error: DatabaseError) {

     }
}

database.addValueEventListener(getData)

答案1

得分: 1

问题出在 myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)。 你总是在整个字符串的开头设置图像跨度。 做一些类似这样的事情:

override fun onDataChange(snapshot: DataSnapshot) {

    val builder = SpannableStringBuilder()

    snapshot.children.forEach { data ->
        builder.append("$data#") // the `#` doesn't mind and will be replaced with the image span

        val span = context?.let {
            ImageSpan(it, R.drawable.myImage, DynamicDrawableSpan.ALIGN_BASELINE)
        }

        builder.setSpan(
            span,
            builder.length - 1,
            builder.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        builder.append("\n\n") // at the end, append new lines
    }
    
    textView.text = builder
}
英文:

The problem is at myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE).
You always set the image span at the first of the whole string. Do something like this:

override fun onDataChange(snapshot: DataSnapshot) {

    val builder = SpannableStringBuilder()

    snapshot.children.forEach { data ->
        builder.append("$data#") // the `#` doesn't mind and will be replaced with the image span

        val span = context?.let {
            ImageSpan(it, R.drawable.myImage, DynamicDrawableSpan.ALIGN_BASELINE)
        }

        builder.setSpan(
            span,
            builder.length - 1,
            builder.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        builder.append("\n\n") // at the end, append new lines
    }
    
    textView.text = builder
}

huangapple
  • 本文由 发表于 2020年9月8日 14:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63788533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定