两格类型
通常问题来自getView。我不确定您真正在寻找什么,因为您没有提供自定义适配器。但是,我给你一个例子,有 2 种不同的单元格类型,但单元格只包含一个文本视图。
在这种情况下,所有单元格都已通过回收充分调整。仔细阅读并扩展/修改以满足您的需求。
在你的CustomAdapter
private final class CustomArrayAdapter extends ArrayAdapter<String> {
//the fastest is to get the layout inflater once and not each time getView is called
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
...
//here depending on the item position determine if cell is of type 1 or 2
@Override
public int getItemViewType(int position) {
if (/* your type 1 criteria is match */) {
return TYPE_1;
} else {
return TYPE_2;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_1:
convertView = mInflater.inflate(R.layout.row_of_type_1, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.textview_type_1);
break;
case TYPE_2:
convertView = mInflater.inflate(R.layout.row_of_type_2, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.textview_type_2);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//get the text for that specific cell and set it
holder.text.setText(getMyCustomTextForThisCell());
return convertView;
}
}
static class ViewHolder {
TextView text;
}
单细胞型
如果您只需要一种类型的单元格,其中包含一个文本视图,并希望在所有单元格中放置不同长度的文本,并且单元格会自动调整其高度,只需在您的 CustomAdapter
private final class CustomArrayAdapter extends ArrayAdapter<String> {
//the fastest is to get the layout inflater once and not each time getView is called
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row_type, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.textview_in_your_row);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//get the text for that specific cell and set it
holder.text.setText(getMyCustomTextForThisCell());
return convertView;
}
}
static class ViewHolder {
TextView text;
}
单元格布局
在示例中,我给出的文件 row_type.xml(您的行的布局文件)应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview_in_your_row"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
不要忘记android:layout_height="wrap_content",否则单元格不会调整大小
旁白
此旁注没有讨论 OP 问题,但是如果人们因为标题中的 细胞回收 词而来到这里,他们可能会对我给出一个完整的回收示例这一事实感兴趣此处带有 EditText 的单元格
The content of EditText contained into a Android ListView isn't saved