Improving spanned/spannable performance in setting TextView RecylcerView
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
TextViewand 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.Factoryobject 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?
java
add a comment |
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
TextViewand 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.Factoryobject 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?
java
add a comment |
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
TextViewand 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.Factoryobject 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?
java
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
TextViewand 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.Factoryobject 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?
java
java
edited Nov 26 '18 at 3:52
Misha Akopov
3,811224362
3,811224362
asked Nov 24 '18 at 16:51
seekingStillnessseekingStillness
1,81921430
1,81921430
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
}
});
Keep in mind, it should be set from ViewHolder's contructor, not onBindViewHolder. When you get reference to it by findViewById.
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460355%2fimproving-spanned-spannable-performance-in-setting-textview-recylcerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
});
Keep in mind, it should be set from ViewHolder's contructor, not onBindViewHolder. When you get reference to it by findViewById.
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
add a comment |
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;
}
});
Keep in mind, it should be set from ViewHolder's contructor, not onBindViewHolder. When you get reference to it by findViewById.
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
add a comment |
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;
}
});
Keep in mind, it should be set from ViewHolder's contructor, not onBindViewHolder. When you get reference to it by findViewById.
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;
}
});
Keep in mind, it should be set from ViewHolder's contructor, not onBindViewHolder. When you get reference to it by findViewById.
edited Nov 25 '18 at 7:05
answered Nov 24 '18 at 17:14
Misha AkopovMisha Akopov
3,811224362
3,811224362
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
add a comment |
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Thanks, so I findViewByID, then add your code, then setText... all in ViewHolder. Correct?
– seekingStillness
Nov 25 '18 at 14:12
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Oops, I meant so I findViewByID, then add your code... in ViewHolde constructor. And setText in onBIndViewHolder
– seekingStillness
Nov 25 '18 at 14:51
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
Exactly ! Last option you wrote is correct way.
– Misha Akopov
Nov 25 '18 at 17:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460355%2fimproving-spanned-spannable-performance-in-setting-textview-recylcerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown