BaseAdapter를 확장하는 맞춤 어댑터 인 FieldAdapter를 만들고 있습니다. TextView와 Spinner 또는 EditText가 포함 된 LinearLayout 뷰의 GridView가 만들어졌습니다.GridView 용 비정형 포맷/뷰가있는 Android 어댑터
많은 것을 살펴본 후에 최적화 된 방법은 ViewHolder 메서드를 사용하는 것입니다. 그러나 이것은 모두 동일한 속성을 공유하는 항목 목록에 최적화 된 것으로 보입니다.
잠재적 인 문제는 TextView와 EditText가있는 뷰가 4 개, TextView와 스피너가있는 뷰가 5 개있어 특정 레이아웃을 취할 수 없다는 것입니다.
각 항목에 대해 어떤 종류의보기를 반환해야하는지 확인하는 논리가 내 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;
}
}