Improving spanned/spannable performance in setting TextView RecylcerView

217
November 25, 2018, at 03:30 AM

I have a RecyclerView with a number of TextViews. One of these TextViews is populated with a Spanned, via a marked up String taken from an ArrayList<Object>. After reading this tutorial by Florina Muntenescu, I realize I can likely improve my performance by using Spannable Factory. My biggest problem is that her code is written in Kotlin (and I'm not quite there yet).

As far as I can tell, this is the key information,

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)

So, suppose I have this simple RecyclerView adapter setting a single Spanned TextView.

@Override
public void onBindViewHolder(RecyclerViewHolder rvh, int position){
    String string =arrayList.get(position).getValue();
    Spanned spanned = getSpannedValue(string);
    rvh.tv.setText(spanned);
}

How can I change my code to utilize the recommendation from Florina?

Answer 1

In java you should just override method newSpannable, in its implementation cast source (CharSequence) to Spannable and set this factory to TextView (tvText in my case)

       tvText.setSpannableFactory(new Spannable.Factory(){
            @Override
            public Spannable newSpannable(CharSequence source) {
                return (Spannable) source;
            }
        });
Rent Charter Buses Company
READ ALSO
Callback onReadRemoteRssi() never gets called

Callback onReadRemoteRssi() never gets called

I am facing some problem on android by reading RSSI from BLE deviceMy code was always working and now i had to modify Gatt connection function to fix some issues and now i cant read RSSI anymore

240
Maintain check box state in Nested Recyclerview [on hold]

Maintain check box state in Nested Recyclerview [on hold]

I want to maintain checkbox state in nested recyclerview(Horizontal RV) and also scroll positionThis should be applicable even if I come out of the activity

157
Recyclerview populated from ViewModel is empty when I navigate back to it

Recyclerview populated from ViewModel is empty when I navigate back to it

My app has one MainActivity with two fragments: The first fragment is: UpcomingViewPagerFragment which contains 12 UpcomingGamesFragments and the second fragment is: FavoriteFragment

156
Android Studio app won&#39;t change its default icon when run on device

Android Studio app won't change its default icon when run on device

I've created a simple icon using Image Asset (only changing the background color and the object using clip art) but it won't show on deviceI've checked all the places where I store the icon (mipmap, android manifest) and they all show the icon I want

175