我目前正在使用for循环在ListView元素内的LinearLayout中添加TextViews,因为我必须添加的TextViews数量是变量。我像往常一样将项目添加到ListViews。 问题是,当我滚动项目开始变得越来越多,当我向上滚动时,我最终得到了数百个项目。我认为这是因为ListView再次重新加载并循环遍历for循环,因此多次添加每个TextView。我可以阻止吗?
我的代码:
public class ListViewAdapter extends ArrayAdapter<RowItem> {
private final Context context;
public ListViewAdapter(Context context, List<RowItem> items) {
super(context, R.layout.list, items);
this.context = context;
}
private class ViewHolder {
TextView title;
LinearLayout linearLayout;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_list, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.list_line);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.list_linear);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(rowItem.getTitle());
String data = rowItem.getData();
String[] split0 = data.split("=");
Log.e("DATA", data);
Log.e("LINE", rowItem.getTitle());
for (int j = 0; j < split0.length; j++) {
String[] split1 = split0[j].split(":");
LinearLayout parentLayout = new LinearLayout(MainActivity.context);
parentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int k = 0; k < split1.length; k++) {
TextView textView = new TextView(MainActivity.context);
textView.setText(split1[k]);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
textView.setTextSize(18);
parentLayout.addView(textView);
}
holder.linearLayout.addView(parentLayout);
}
return convertView;
}
}
我的数据情景对于一个条目看起来像这样:
text1:text2=text3:text4
它在列表中的显示方式:
text1 text2
text3 text4
答案 0 :(得分:3)
问题是每次行可见时,ArrayAdapter都会调用getView()
。如果之前已设置该行,则会传递convertView
参数中先前设置的视图。
有关详细信息,请参阅here。来自convertView
的{{1}}:
如果可能的话,重用旧视图。注意:你应该检查一下 在使用之前,view是非null且具有适当的类型。如果不是 可以转换此视图以显示正确的数据,此方法 可以创建一个新视图。异类列表可以指定它们的数量 视图类型,以便此视图始终是正确的类型(请参阅 getViewTypeCount()和getItemViewType(int))。
编辑:由于提出的原始代码导致了问题,看起来您可能无法使用标准视图持有者设计模式。您有一个独特的设置,您可以在其中创建每行中的动态数量的项目。
您可能可以执行略微优化的操作,您无需在每次inflate()
来电时致电getView()
,但每次都会重新创建ViewHolder
调用getView()
,以便在滚动时填充正确的数据:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_list, null);
}
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.list_line);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.list_linear);
convertView.setTag(holder);
holder.title.setText(rowItem.getTitle());
String data = rowItem.getData();
String[] split0 = data.split("=");
Log.e("DATA", data);
Log.e("LINE", rowItem.getTitle());
for (int j = 0; j < split0.length; j++) {
String[] split1 = split0[j].split(":");
LinearLayout parentLayout = new LinearLayout(MainActivity.context);
parentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int k = 0; k < split1.length; k++) {
TextView textView = new TextView(MainActivity.context);
textView.setText(split1[k]);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
textView.setTextSize(18);
parentLayout.addView(textView);
}
holder.linearLayout.addView(parentLayout);
}
return convertView;
}
如果这不起作用,您可以回到基础,只需在每次调用getView()
时完全重新创建视图:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_list, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.list_line);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.list_linear);
convertView.setTag(holder);
holder.title.setText(rowItem.getTitle());
String data = rowItem.getData();
String[] split0 = data.split("=");
Log.e("DATA", data);
Log.e("LINE", rowItem.getTitle());
for (int j = 0; j < split0.length; j++) {
String[] split1 = split0[j].split(":");
LinearLayout parentLayout = new LinearLayout(MainActivity.context);
parentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
parentLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int k = 0; k < split1.length; k++) {
TextView textView = new TextView(MainActivity.context);
textView.setText(split1[k]);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
textView.setTextSize(18);
parentLayout.addView(textView);
}
holder.linearLayout.addView(parentLayout);
}
//}
return convertView;
}