2010-09-30 19 views
28

Ecco il mio ArrayAdapter. Vorrei rendere questo più efficiente seguendo il modello ViewHolder:Come posso far muovere il mio ArrayAdapter seguendo il pattern ViewHolder?

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

ma non sono sicuro di come per ottenere questo risultato.

UPDATE: ViewHolder modello

private class QuoteAdapter extends ArrayAdapter<Quote> { 

    private ArrayList<Quote> items; 
    // used to keep selected position in ListView 
    private int selectedPos = -1; // init value for not-selected 

    public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 

    public void setSelectedPosition(int pos) { 
     selectedPos = pos; 
     // inform the view of this change 
     notifyDataSetChanged(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; // to reference the child views for later actions 

     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.mainrow, null); 

      // cache view fields into the holder 
      holder = new ViewHolder(); 
      holder.nameText = (TextView) v.findViewById(R.id.nameText); 
      holder.priceText = (TextView) v.findViewById(R.id.priceText); 
      holder.changeText = (TextView) v.findViewById(R.id.changeText); 

      // associate the holder with the view for later lookup 
      v.setTag(holder); 
     } 
     else { 
      // view already exists, get the holder instance from the view 
      holder = (ViewHolder)v.getTag(); 
     } 

     // change the row color based on selected state 
     if (selectedPos == position) { 
      v.setBackgroundResource(R.drawable.stocks_selected_gradient); 
      holder.nameText.setTextColor(Color.WHITE); 
      holder.priceText.setTextColor(Color.WHITE); 
      holder.changeText.setTextColor(Color.WHITE); 
     } else { 
      v.setBackgroundResource(R.drawable.stocks_gradient); 
      holder.nameText.setTextAppearance(getApplicationContext(), R.style.BlueText); 
      holder.priceText.setTextAppearance(getApplicationContext(), R.style.BlueText); 
      holder.changeText.setTextAppearance(getApplicationContext(), R.style.BlueText); 
     } 

     Quote q = items.get(position); 
     if (q != null) { 
      if (holder.nameText != null) { 
       holder.nameText.setText(q.getSymbol()); 
      } 
      if (holder.priceText != null) { 
       holder.priceText.setText(q.getLastTradePriceOnly()); 
      } 
      if (holder.changeText != null) { 
       try { 
        float floatedChange = Float.valueOf(q.getChange()); 
        if (floatedChange < 0) { 
         if (selectedPos != position) 
          holder.changeText.setTextAppearance(getApplicationContext(), R.style.RedText); // red 
        } else { 
         if (selectedPos != position) 
          holder.changeText.setTextAppearance(getApplicationContext(), R.style.GreenText); // green 
        } 
       } catch (NumberFormatException e) { 
        System.out.println("not a number"); 
       } catch (NullPointerException e) { 
        System.out.println("null number"); 
       } 
       holder.changeText.setText(q.getChange() + " (" + q.getPercentChange() + ")"); 
      } 
     } 
     return v; 
    } 
} 
+0

Ho creato uno strumento online per generare il codice ViewHolder da un layout XML: http://www.buzzingandroid.com/tools/android-layout-finder/ – JesperB

risposta

44

Il ViewHolder è fondamentalmente un'istanza di classe statica che si associa con una vista quando è creato, la memorizzazione nella cache il bambino vede che stai guardando in fase di esecuzione. Se la vista esiste già, recupera l'istanza del titolare e utilizza i suoi campi invece di chiamare findViewById.

Nel tuo caso:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    ViewHolder holder; // to reference the child views for later actions 

    if (v == null) { 
     LayoutInflater vi = 
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.mainrow, null); 
     // cache view fields into the holder 
     holder = new ViewHolder(); 
     holder.nameText = (TextView) v.findViewById(R.id.nameText); 
     holder.priceText = (TextView) v.findViewById(R.id.priceText); 
     holder.changeText = (TextView) v.findViewById(R.id.changeText); 
     // associate the holder with the view for later lookup 
     v.setTag(holder); 
    } 
    else { 
     // view already exists, get the holder instance from the view 
     holder = (ViewHolder) v.getTag(); 
    } 
    // no local variables with findViewById here 

    // use holder.nameText where you were 
    // using the local variable nameText before 

    return v; 
} 

// somewhere else in your class definition 
static class ViewHolder { 
    TextView nameText; 
    TextView priceText; 
    TextView changeText; 
} 

avvertimento: non ho provato a compilare questo, in modo da prendere con un grano di sale.

+0

Questo tipo di ha a che fare con una domanda che ho chiesto in precedenza viste di riciclaggio. Sei a conoscenza di eventuali analisi delle prestazioni eseguite sul sovraccarico della creazione di viste? – Falmarri

+0

@Falmarri Non specificamente, no. Questo particolare schema è per evitare l'inefficienza di 'findViewById' piuttosto che la creazione di View. Sarebbe semplice fare un caso di prova per fare la propria analisi di riutilizzare le visualizzazioni rispetto alla creazione, comunque. Sospetto che l'analisi del solo layout XML sarebbe una causa sufficiente per garantire il riutilizzo. Se la vista è molto semplice e ha un layout programmatico, non ho una conoscenza diretta dell'overhead coinvolto nella creazione. – codelark

+0

ottimo codice di esempio. Ho implementato il pattern del view view. Controlla v.setBackgroundResource() lo sto facendo correttamente in relazione al pattern View Holder? –

Problemi correlati