java - 提高设置 TextView RecyclerView 时的跨度/可跨度性能

标签 java android android-recyclerview textview spannable

我有一个RecyclerView数量为TextViews 。其中之一TextViews填充有 Spanned ,通过标记 String取自ArrayList<Object> 。看完this在 Florina Muntenescu 的教程中,我意识到我可以通过使用 Spannable Factory 来提高我的表现。我最大的问题是她的代码写在 Kotlin (我还没有完全做到这一点)。

据我所知,这是关键信息,

Let’s say that we want to reuse a TextView and set the text multiple times, like in a RecyclerView.ViewHolder...

val spannableFactory = object : Spannable.Factory() {
    override fun newSpannable(source: CharSequence?): Spannable {
        return source as Spannable
    }
}`

Set the Spannable.Factory object once right after you get a reference to your TextView. If you’re using a RecyclerView, do this when you first inflate your views.

textView.setSpannableFactory(spannableFactory)

所以,假设我有这个简单的 RecyclerView adapter设置单个Spanned TextView .

@Override
public void onBindViewHolder(RecyclerViewHolder rvh, int position){

    String string =arrayList.get(position).getValue();
    Spanned spanned = getSpannedValue(string);
    rvh.tv.setText(spanned);
}

如何更改我的代码以利用 Florina 的建议?

最佳答案

在java中,您应该只重写方法newSpannable,在其实现中将源(CharSequence)强制转换为Spannable,并将该工厂设置为TextView(在我的例子中为tvText)

       tvText.setSpannableFactory(new Spannable.Factory(){
            @Override
            public Spannable newSpannable(CharSequence source) {
                return (Spannable) source;
            }
        });

请记住,它应该在ViewHolder的构造函数中设置而不是onBindViewHolder。当您通过 findViewById 获取对它的引用时。

关于java - 提高设置 TextView RecyclerView 时的跨度/可跨度性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53460355/

相关文章:

java - 未知错误 : DevTools Active Port file doesn't exist java selenium

java - 如何以编程方式检查哪个 sim 卡被设置为 android 中的默认 sim 卡

java - 通过API从服务器传输数据完成后如何使用远程数据构建recyclerview

java - 没有找到适合 BufferedWriter(String) 的构造函数

java - 使用 mockito 跳过静态方法调用

c# - Java 和 C# 的并行开发

Android AlarmManager 随机安排通知?

Android Studio/Kotlin - 无效错误 "Overriding method should call super"

android - 漏洞 : exchange between fragments

android - 工具 :listitem for custom view extending RecyclerView