0

I am new to android and I want to add a long list of textViews to a LinearLayout (about 200 textviews).

As you know adding large views to layout causes the Activity to freeze until all of textviews are added. I want to refresh the layout after each addview and show the new textview that is added to the layout. I have tested everything that came to my mind and I've searched all the web for answer and I know that I must use threads, but none of threads worked for me.

I have used ListView but it is too slow because it needs to make so many control invisible when they are out of view.

How can I force a refresh after each view?

Edit:
Here is the layout that is being inflated about 100 times:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp" >

<com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/itemTitle1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text=""
    android:textColor="#58595b"
    android:textSize="@dimen/details_title_font_size"
    pixlui:typeface="font.ttf" />

<JustifiedTextView
    android:id="@+id/itemText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textColor="#58595b"
    android:textSize="@dimen/details_font_size"
    android:lineSpacingMultiplier="2"/>
</LinearLayout>

And here is getView method of my BaseAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {


        convertView = mInflater.inflate(R.layout.rowitemview, null);
        holder = new ViewHolder();
        holder.title1 = (TextView) ((LinearLayout)convertView).getChildAt(0);
        holder.text1 = (JustifiedTextView)  ((LinearLayout)convertView).getChildAt(1);
        holder.text1.SetTextAlign(Align.RIGHT);
        holder.text1.setTypeface(font);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.title1.setText(getItem(position).getTitle()); 
    holder.text1.setText(getItem(position).getDesc());

    return convertView;
}

The problem with this ListView is that it is very laggy when scrolling up or down. I even tried lazy loading items, but it didn't work either.

rene
  • 41,474
  • 78
  • 114
  • 152
miladrasooli
  • 115
  • 1
  • 10

1 Answers1

3

You really need to refactor your code to not add 200 textviews.

What you can do is replace those 200 textviews with one listView. This adds and recycles child views dynamically. So even if you have 200 entries, the app will only occupy itself with what needs to be drawn on the screen.

See listview tutorial.

NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • I edited my question and I added some of my code for `ListView` – miladrasooli Mar 06 '14 at 19:20
  • I see... "I have used ListView but it is too slow because it needs to make so many control invisible when they are out of view." This isn't what listViews do, the Views you can't see actually don't exist, they are recycled, and written on top of. – NameSpace Mar 06 '14 at 19:22
  • Thanks for the info... But still it doesn't solve my problem... the ListView is still very laggy , On the other hand `ScrollView` is very smooth but it takes around5 seconds on my nexus 7 to add all of the textviews... If there was a way to add views to the layout in the background thread that user doesn't have to wait until all of controls are loaded would be great... – miladrasooli Mar 06 '14 at 19:30
  • unfortunately, i can't see what's lagging you from from this... In general listviews aren't directly effected by size of data, because they are only a view as to a slice of that data -- i've used them to display databases with 1000's of items. I've noticed you're using custom textViews...its hard to say what the code in there is doing in there (if anything) as you scroll. – NameSpace Mar 06 '14 at 19:37
  • All the text views have to be loaded on the UI thread (i beleive) so a background thread I don't see helping. Background threads are usually used to load an image from a file (for example), then set that image to an imageView on the UI thread. Here i can't see what you can factor out...really the code above doesn't show what's lagging you. – NameSpace Mar 06 '14 at 19:42
  • I just tested and found out that the problem is `JustifiedTextView` . Is there any chance that you know a good and not CPU consuming way to justify text in android (for my case I don't think I can use `WebView` , can I?) ? – miladrasooli Mar 06 '14 at 19:51
  • I can't recommend anything. The only justification solutions i see are custom, which I haven't used. You can look at this [thread](http://stackoverflow.com/questions/11922861/how-to-justify-text-on-a-textview-made-easy-android) for some that people have used. – NameSpace Mar 06 '14 at 20:05