異なる行レイアウト テンプレートを使用したリスト ビューごとに、同じことを行うカスタム アダプターを作成する必要があります。xml 行レイアウトを読み込み、ID でコントロール (TextView、ImageView など) を取得し、データを表示します。このようなものです。 :
public class CommentAdapter extends BaseAdapter {
protected Activity activity;
protected static LayoutInflater layoutInflater = null;
protected List<Comment> lst;
public CommentAdapter(Activity activity, List<Comment> lst){
this.activity = activity;
this.lst = lst;
layoutInflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return lst.size();
}
public Object getItem(int position) {
return lst.get(position);
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView textName;
public TextView textComment;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder viewHolder;
if (v == null) {
v = layoutInflater.inflate(R.layout.listitem, null);
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) v.findViewById(R.id.txtName);
viewHolder.image = (ImageView) v.findViewById(R.id.icon);
viewHolder.textComment = (TextView)v.findViewById(R.id.txtComment);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
}
Static.overrideFonts(v);
viewHolder.image.setBackgroundResource(lst.get(position).Icon);
viewHolder.textName.setText(lst.get(position).Name);
viewHolder.textComment.setText(lst.get(position).Comment);
return v;
}
}
多くの種類のリスト ビュー (差分行レイアウト テンプレート) では、多くのアダプターを作成する必要があります。
したがって、問題は、行 xml を動的にロードし、その ID に基づいてビュー コントロールをマップすることができる 1 つのテンプレート アダプターを作成することです (おそらくリフレクトを使用します)。行 xml レイアウト、コントロール ID、ビュー コントロールは別の場所で定義されます。
はありますかdesign pattern
、example
またはframework
これを達成できますか?