2016-03-28 52 views
1

Verileri görüntülemek için bir metin görünümü olan özel bir liste görünümü var, bellek kullanımını en iyi duruma getirmek için görünüm tutucu desenini kullanıyorum, ancak sürekli liste görünümünde kaydırma yaparken, bellek ayırma artışı var (android monitör belleğinde) android stüdyo içinde)Liste görünümü bellek kullanımı

Sorunu nasıl çözmeliyim?

@Override 
public View getView(int position, View view, ViewGroup parent) { 

    ViewContainer viewContainer; 
    View rowView = view; 

    if(rowView==null){ 

     LayoutInflater inflater = context.getLayoutInflater(); 
     rowView= inflater.inflate(R.layout.lv2rowlayout, null, true); 

     viewContainer = new ViewContainer(); 
     //---get a reference to all the views on the xml layout--- 

     viewContainer.txten = (TextView) rowView.findViewById(R.id.txten); 
     viewContainer.txtfars = (TextView) rowView.findViewById(R.id.txtfars); 



     String s ; 
     Typeface custom_font; 

     viewContainer.txten.setTextSize(TypedValue.COMPLEX_UNIT_SP,sd.enSize); 
     s="fonts/"+sd.enFont; 
     custom_font = Typeface.createFromAsset(context.getAssets(),s); 
     viewContainer.txten.setTypeface(custom_font); 
     viewContainer.txten.setTextColor(sd.enColor); 

     viewContainer.txtfars.setTextSize(TypedValue.COMPLEX_UNIT_SP,sd.farssize); 
     s="fonts/"+sd.farsFont; 
     custom_font = Typeface.createFromAsset(context.getAssets(),s); 
     viewContainer.txtfars.setTypeface(custom_font); 
     viewContainer.txtfars.setTextColor(sd.farsColor); 

     rowView.setTag(viewContainer); 
    } 
    else { 
     viewContainer = (ViewContainer) rowView.getTag(); 

    } 

    //---customize the content of each row based on position--- 
    viewContainer. txten.setText(en[position]); 
    viewContainer.txtfars.setText(fars[position]); 

    return rowView; 
} 
+0

Yazı tipiniz pozisyona bağlı olmadığından, neden global bir değişken bildirip liste görünümü oluşturucuda başlatmıyorsunuz? – Kaustuv

+0

evet doğru yoldan teşekkürler u –

cevap

2

Bazı cihazlarda hafıza sızıntıları neden olabileceğini (https://code.google.com/p/android/issues/detail?id=9904) hakkında bilinen bir sorun vardır o Typeface.createFromAsset(...) hakkında, öyle ListView ilgili değil düşünüyorum. Böyle Typeface oluşturma önbelleğe alabilir:

public class FontCache { 

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>(); 

    public static Typeface get(String name, Context context) { 
     Typeface tf = fontCache.get(name); 
     if(tf == null) { 
      try { 
       tf = Typeface.createFromAsset(context.getAssets(), name); 
      } 
      catch (Exception e) { 
       return null; 
      } 
      fontCache.put(name, tf); 
     } 
     return tf; 
    } 
} 
+0

Bunun yerine: 's =" fontlar/"+ sd.enFont; custom_font = Typeface.createFromAsset (context.getAssets(), s); viewContainer.txten.setTypeface (custom_font); Yazıyüzünü bununla ayarlayın: 's =" fontlar/"+ sd.enFont; viewContainer.txten.setTypeface (FontCache.get (s, bağlam)); ' – GaborNovak

0

deneyin olarak gösterilen seti için aşağıdaki Adaptörü kurucuları içindedir custom_font = Typeface.createFromAsset(context.getAssets(),s); pasajı içinde.