我有一个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 aRecyclerView.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/