1

I have a RelativeLayout above my ListView, and I'm expecting the TextView on that layout to change based on the content of the first visible item of the ListView. So I'm using onScrollListenerwith my list:

@Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (adapter != null && adapter.getCount() > 0) {
            Transaction data = (Transaction) adapter.getItem(firstVisbileItem);
            Date date = data.getTransactionDate();
            date.setActualDate(date.getActualDate());
            String month = date.getMonthString(context);
            String year = date.getYear() + "";
            tvLeftValue.setText(month);
            tvRightValue.setText(year);
        }       
    }

It's just really weird that when I use this, the list scrolls really slowly, but when I tried using setText("") for both TextView, the list scrolls smoothly as normal. This problem occurs when I test on real device (Galaxy S3), not on Genymotion's emulator. Why could such a weird problem happen? Is there any possible solution to this? If any of you have ever encountered this, please help me and thanks in advance.

EDIT: Actually this also happens in Genymotion's emulator, it's just faster than in real devices so I thought it didn't happen.

huong
  • 4,534
  • 6
  • 33
  • 54
  • did you get any resolution for this or find a work-around? – Dittimon Jun 17 '14 at 23:20
  • 1
    @Dittimon actually i didn't find any nice solution to this so I had to switch to use [Amazing ListView](https://code.google.com/p/android-amazing-listview/) to make the section headers for my listview. – huong Jun 18 '14 at 02:08
  • You may just be setting this way too frequently and causing unneeded redraws. It may not work, but you can try setting the text only if its changed. – Gabe Sechan Jul 01 '14 at 03:53

2 Answers2

0

The onScorll method is called very frequently. You are doing too much thing on main thread.Try use working Thread to do the compute work and Handler to update UI.

Marco Lee
  • 121
  • 5
  • Thank you, I have tried using `AsyncTask` but it didn't improve the situation. Problem is, I tried commenting out all the lines that are responsible for getting data and leaving the `setText` with random string but the scrolling is still slow. – huong Sep 16 '13 at 10:07
0

This ended up solving a similar issue I was having Simple TextView.setText causes 40% CPU Usage

Basically, the accepted answer is suggesting that setting the text forces a refresh of the whole layout because the TextView may have changed size.

The solution is fix the size of your textView. You still may be able to use width as match parent depending on your layout.

<TextView
     android:layout_width="200dp"
     android:layout_height="32dp"
     android:ellipsize="end" />
Community
  • 1
  • 1
Dittimon
  • 986
  • 2
  • 14
  • 28
  • Also possible would be using a relative layout where the text view is scaled to fit between 2 elements- that would prevent it from shrinking. – Gabe Sechan Jul 01 '14 at 03:52