anony_root
anony_root

Reputation: 1527

How to keep list items in memory?

I have custom listview. When I scroll my listview, android keeps in memory (as far as I understand) items which is displaying on screen and doesn't keep items which is hidden (not scrolled to).

In my case (I think) keeping all list items would be better than generating hidden items.

So, how to "tell" android to keep all items in memory? (15-20 items). PS: if it's wasting of resources, I'd like just to try.

My adapter (some funcs):

private View newView(Context context, ViewGroup parent) {
  LayoutInflater layoutInflater = LayoutInflater.from(context);
  return layoutInflater.inflate(R.layout.myl,parent,false);
}
public View getView(int position,View convertView,ViewGroup parent) {
  View view=null;
  if(convertView!=null) view=convertView; else view=newView(context,parent);
  HashMap<String,String> d=new HashMap<String,String>();
  d=data.get(position); 
  String qweqwe=d.get("qweqwe"); //9 more lines like this.
  TextView txt=(TextView)view.findViewById(R.id.mfmf); //
  txt.setText(qweqwe); //
  txt.setTypeface(mlf); //5 more blocks of 3 lines like this.
  if (smth.equals("0")){
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.mvmv));
  } else {
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.mvmv2));
  }
  return view;
}

Upvotes: 0

Views: 1176

Answers (2)

Ole
Ole

Reputation: 7979

Ok.. there are some things that can be optimized here, instead of trying to fix lag with workarounds :)

You should implement a static class, where you can store references to the Views in your myl.xml. For each View you want to manipulate in myl.xml, you create a View in this static class. So if you have 10 TextViews, you fill this class with 10 TextViews.

static class AdapterViewsHolder {
    TextView txt1;
    TextView txt2;
    TextView txt3;
    ...
    ImageView img1;
    ... etc etc.
}

In the adapter, you now only do the findViewById() calls if the convertView is null. findViewById() is not cheap, so limiting the amount of calls increases performance.

private HashMap<String, String> mData;
private LayoutInflater mInflater;
private TypeFace mCustomTypeFace;

// Some contructor for passing data into the Adapter.
public BaseAdapter(HashMap<String, String> data, Context ctx) {
    mData = data;
    mInflater = LayoutInflater.from(ctx);
    mCustomTypeFace = Typeface.createFromAsset(ctx.getAssets(), "yourTypeFace.ttf");
}

public View getView(int position, View convertView, ViewGroup parent) {
    // This AdapterViewsHolder will hold references to your views, so you don't have to 
    // call findViewById() all the time :)
    AdapterViewsHolder holder;

    // Check if convertView is null
    if(convertView == null) {
        // If it is, we have to inflate a new view. You can probably use the newView() call here if you want.
        convertView = mInflater.inflate(R.layout.myl, null);

        // Initialize the holder
        holder = new AdapterViewsHolder();

        // Now we do the smart thing: We store references to the views we need, in the  holder. Just find all the views you need, by id.
        holder.txt1 = (TextView) convertView.findViewById(R.id.textview1);
        holder.txt2 = (TextView) convertView.findViewById(R.id.textview2);
        ...
        holder.img1 = (ImageView) convertView.findViewById(R.id.imageview1);

        // Store the holder in the convertViews tag
        convertView.setTag(holder);
    }
    else {
        // If convertView is not null, we can get get the holder we stored in the tag.
        // This holder now contains references to all the views we need :)
        holder = (AdapterViewsHolder) convertView.getTag();
    }



    // Now we can start assigning values to the textviews and the imageviews etc etc
    holder.txt1.setText(mData.get(position));
    ...
    holder.txt1.setTypeface(mCustomTypeFace);
    ...
    holder.img1.setImageResource("IMAGE RESOURCE HERE");  

    if(someThing.equals("sometext") {
         convertView.setBackgroundDrawable(somedrawable);
    }
    else {
         convertView.setBackgroundDrawable(someotherdrawable);
    }

    // Finally, we return the convertView
    return convertView;
}

I do not know how your data is organized, so you have to change this code a bit.

One more thing that can cause lag is the android:cacheColorHint xml attribute. Usually you set this to either the same color as you application background, or transparent. Setting it transparent have been known to cause rapid Garbage collections on some occasions.

Upvotes: 2

FoamyGuy
FoamyGuy

Reputation: 46856

You could override the Adapter and have it inflate all of the views in the constructor, then just return the proper one with getView(). Might make it easy if you store the Views in some data object (array, list etc..)

But really you should let the system use the convertView like it was designed to. Overall you'd get better performance doing it that way I think.

Upvotes: 0

Related Questions