0

I'm trying to create RecyclerView item programmatically using Anko library and I want to do all the stuff in one RelativeLayout for the best perfomance.

The title of my item should be match_parent, but it shouldn't cover flag marker (hasImportantMessages) or number on the right (issues counter), so I'm using this code to adjust RelativeLayout rules:

with(tvTitle.layoutParams as RelativeLayout.LayoutParams) {
        removeRule(START_OF)
        removeRule(ALIGN_PARENT_END)

        when {
            hasImportantMessages -> addRule(START_OF, R.id.iv_important_messages)
            issueCounter > 0 -> addRule(START_OF, R.id.tv_issues_counter)
            else -> addRule(ALIGN_PARENT_END)
        }
    }

When RecyclerView creates first visible ViewHolders everything is fine, but when I scroll down a little and RecyclerView starts to reuse it's holders, I got some issues with it, check these screenshots.

Good

Bad

As you can see, RelativeLayout rules don't work properly. Title textView overlaps flag marker or issues counter textView. Maybe I should invalidate() somehow, but I already tried - doesn't work.

Den
  • 1,284
  • 2
  • 14
  • 33
  • any specific reason for not doing it in xml? – pskink Sep 03 '18 at 07:35
  • Optimization. LayoutInflater uses lot of resourses and it takes some time to inflate xml. – Den Sep 03 '18 at 08:40
  • and how many times it is called? maybe 6 max 8 times, does it really pay off? i dont think so... – pskink Sep 03 '18 at 08:43
  • Well, I can notice my RV lags a little bit with the first scrolling because it creates few ViewHolders to fill the pool. Also onCreateViewHolder called at least 3-4 times if I use smoothScrollToPosition(0) from the end of my list, I don't know why tho. – Den Sep 03 '18 at 08:46
  • so how many times `onCreateViewHolder` is called in total? btw you can create a small pool of views using `LayoutInflater` so you dont have to use `inflate` method inside `onCreateViewHolder` method – pskink Sep 03 '18 at 08:48
  • 6-10 times after I attach my adaper and then 3-4 times everytime I use smoothScrollToPosition(0). – Den Sep 03 '18 at 08:48

1 Answers1

0

Did you try to use updateViewLayout after updating rules?

Here is example: https://stackoverflow.com/a/6801082/6055194

Eugene Babich
  • 1,271
  • 15
  • 25
  • The radical solutions are 1) To use multiple layouts https://stackoverflow.com/a/26245463/6055194 2) Replace RelativeLayout by ConstraintLayout – Eugene Babich Sep 03 '18 at 09:13