我有4种不同类型的行,每种行都有自己的xml布局。我该如何管理它们?
我继续只获得第一行类型,而不是其他类型。然后我想象我也必须处理回收。但我不知道怎么做。我放下了一些代码,但是我很遗憾地遗漏了一些东西。感谢
这是代码:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildTypeCount() {
// TODO Auto-generated method stub
Log.i("getChildTypeCount()", "getChildTypeCount()");
return (4);
}
@Override
public int getGroupTypeCount() {
// TODO Auto-generated method stub
return super.getGroupTypeCount();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
int itemType = getChildType(groupPosition,childPosition);
DescrizioneViewHolder descrizioneViewHolder = new DescrizioneViewHolder();
DistanzaViewHolder distanzaViewHolder = new DistanzaViewHolder();
CategoriaViewHolder categoriaViewHolder = new CategoriaViewHolder();
OrdinaViewHolder ordinaViewHolder = new OrdinaViewHolder();
Log.i("groupPosition", Integer.toString(groupPosition));
Log.i("childPosition", Integer.toString(childPosition));
Log.i("itemType", Integer.toString(itemType));
Log.i("----", "----");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (itemType) {
case 0:
convertView = inflater.inflate(R.layout.listview_item_filter_descrizione, null);
descrizioneViewHolder.etDescrizione = (EditText) convertView.findViewById(R.id.etDescrizione);
break;
case 1:
convertView = inflater.inflate(R.layout.listview_item_filter_distanza, null);
distanzaViewHolder.etIndirizzo = (EditText) convertView.findViewById(R.id.etIndirizzo);
distanzaViewHolder.btnCercaIndirizzo = (Button) convertView.findViewById(R.id.btnCercaIndirizzo);
distanzaViewHolder.tvRaggioAzione = (TextView) convertView.findViewById(R.id.tvRaggioAzione);
distanzaViewHolder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
break;
case 2:
convertView = inflater.inflate(R.layout.list_item_filter, null);
categoriaViewHolder.tvCategoria = (TextView) convertView.findViewById(R.id.lblListItem);
categoriaViewHolder.tvCategoria.setText(childText);
break;
case 3:
convertView = inflater.inflate(R.layout.listview_item_filter_ordina, null);
ordinaViewHolder.tvOrdina = (TextView) convertView.findViewById(R.id.tvOrdina);
ordinaViewHolder.btnDecrescente = (Button) convertView.findViewById(R.id.btnDecrescente);
ordinaViewHolder.btnCrescente = (Button) convertView.findViewById(R.id.btnCrescente);
ordinaViewHolder.tvOrdina.setText(childText);
break;
}
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setTextSize(25);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public static class DescrizioneViewHolder {
public EditText etDescrizione;
}
public static class DistanzaViewHolder {
public EditText etIndirizzo;
public Button btnCercaIndirizzo;
public TextView tvRaggioAzione;
public RadioGroup radioGroup1;
}
public static class CategoriaViewHolder {
public TextView tvCategoria;
}
public static class OrdinaViewHolder {
public TextView tvOrdina;
public Button btnDecrescente;
public Button btnCrescente ;
}
}
答案 0 :(得分:1)
我没有看到ViewHolder与你用于它的类和convertView本身之间有任何关联,这意味着ViewHolder没有附加到ViewGroup。 E.g。
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(getLayoutResourceId(), null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
还记得在convertView不为null时设置值。 随后会对列表中的所有主题重复调用getChildView。 示例续:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
int itemType = getChildType(groupPosition,childPosition);
switch (itemType) {
case 0:
return getChildViewDistanza(groupPosition, childPosition, isLastChild, convertView, parent);
break;
case 1:
....
}
return convertView;
}
public View getChildViewDistanza(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
DistanzaViewHolder distanzaViewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_item_filter_descrizione, null);
DistanzaViewHolder distanzaViewHolder = new DistanzaViewHolder();
convertView.setTag(distanzaViewHolder);
}
else {
distanzaViewHolder = convertView.getTag();
}
descrizioneViewHolder.etDescrizione = (EditText) convertView.findViewById(R.id.etDescrizione);
return convertView;
}
如果您想摆脱有关布局的重复元素 - 膨胀和设置标记,如果您在ViewHolder中使用公共基类,则可以轻松地为此编写方法:
protected View inflateLayoutAndSetTag(int resourceID, BaseViewHolder holder) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(resourceID) , null);
convertView.setTag(holder) );
return convertView;
}
并将其命名为:
if (convertView == null) {
DistanzaViewHolder distanzaViewHolder = new DistanzaViewHolder();
convertView = inflateLayoutAndSetTag(R.layout.listview_item_filter_descrizione, distanzaViewHolder);
}
实际上你甚至可以做一些事情,比如
protected <TViewHolder> Pair<View, TViewHolder> getViewHolderAndConvertView(View convertView, int resourceID, TViewHolder dummy) { /** TViewHolder is a type - parameter that is actually unused, just needed for the instanziation of the right type and the pair - type ***/
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resourceID, null);
TViewHolder viewHolder = TViewHolder.class.newInstance();
convertView.setTag(viewHolder);
}
else {
viewHolder = convertView.getTag();
}
return new Pair<View, TViewHolder>(convertView, viewHolder);
}
并将其称为例如:
Pair<View, DistanzaViewHolder> pair = getViewHolderAndConvertView(convertView, R.layout.listview_item_filter_descrizione, (DistanzaViewHolder) null);
DistanzaViewHolder viewHolder = pair.second;
convertView = pair.first;
删除所有重复代码。 但这有点超出了这个问题的范围。 ;)
答案 1 :(得分:0)
您缺少getChildType()
的实施方案。您必须实现此操作才能确定哪个子类型。