0

我正在製作一個擴展BaseAdapter的自定義Adapter,FieldAdapter。我創建了一個包含TextView和一個Spinner或一個EditText的LinearLayout視圖的GridView。具有不規則格式的Android適配器/ GridView的視圖

看來,經過多次調查,優化後的方法是使用ViewHolder方法。但是這似乎是對所有共享相同屬性的項目列表進行了優化。

是什麼讓這個潛在的問題是,我可能有4個視圖的TextView和EditText,另一個5包含一個TextView和一個微調,所以我不能假設一定的佈局。

在我的getView方法中使用邏輯來檢查每個項目需要返回哪種視圖還是有更好的方法是最理想的嗎?我應該只有ViewHolder對象來保存相應的佈局?即DropdownViewHolder和TextViewHolder?

這是否渲染convertView無用?

以下是我的代碼,但我注意到如果我有大量項目列表並快速上下滾動我的整個列表消失,這導致我相信我沒有做正確的事情。

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

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     LinearLayout layoutSection = (LinearLayout) inflater.inflate(R.layout.dropdown, null); 

     TextView labelText = new TextView(this.context); 
     labelText.setText(data.get(position).getLabelText()); 
     labelText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     layoutSection.addView(labelText); 

     if (data.get(position).getFieldType().equalsIgnoreCase("dropdown")) { 
      Spinner spinner = new Spinner(this.context); 
      spinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
      // Options 
      final List<String> options = new ArrayList<String>(); 
      ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context, 
        android.R.layout.simple_list_item_1, options); 
      spinner.setAdapter(spinnerAdapter); 


      layoutSection.addView(spinner); 
     } else if (data.get(position).getFieldType().equalsIgnoreCase("text")) { 
      EditText textbox = new EditText(this.context); 
      textbox.setText("Hi"); 
      textbox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
      layoutSection.addView(textbox); 
     } 

     return layoutSection; 
    } else { 
     return convertView; 
    } 
} 

回答

1

看到那些方法:getItemViewType & getViewTypeCount

相關問題