我有一个嵌套数组显示在我的Listview中.ListView的每一行都有一个水平LinerLayout到HorizontalScrollView。
目前我正在使用ViewHolder模式,并在ListView的getView方法中为LinerLayout动态添加视图。
问题是滚动时列表视图滞后..
这是我的自定义适配器类
public class ChannelArrayAdapter extends ArrayAdapter<Kanal> {
Context context;
List<Kanal> list;
List<Program> pList;
LayoutInflater inflator;
public ChannelArrayAdapter(Context context, List<Kanal> lis) {
super(context, R.layout.channels, lis);
// TODO Auto-generated constructor stub
this.list = lis;
this.context = context;
}
static class ViewHolder {
ImageView imageView;
LinearLayout l;
}
static class ProgramHolder {
TextView programAdi;
TextView programSaati;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.channels, parent, false);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
viewHolder.l = (LinearLayout) convertView.findViewById(R.id.yayinAkisiLayout);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.l.removeAllViews();
}
final Kanal kanal = list.get(position);
pList = kanal.getKanalProgramListesi();
viewHolder.imageView.setImageResource(kanal.getResourcesIkonKanal());
for (Program p : pList) {
View v = new View(context);
v.setId(position);
ProgramHolder pHolder = new ProgramHolder();
v = inflator.inflate(R.layout.programs, null);
pHolder.programSaati = (TextView) v.findViewById(R.id.psaati);
pHolder.programAdi = (TextView)v.findViewById(R.id.pname);
pHolder.programAdi.setText(p.getProgramAdi());
pHolder.programSaati.setText(p.getProgramBaslangic());
v.setTag(pHolder);
viewHolder.l.addView(v);
}
return convertView;
}
}
答案 0 :(得分:0)
将holder设为静态类,只需在getView中编辑其字段即可。您的列表视图滞后,因为每次调用getView时都会创建一些新对象 - &gt;每次加载新项目或获得ListView的可见性。
http://www.youtube.com/watch?v=wDBM6wVEO70
这详细解释了如何制作有效的adapter / listView。
答案 1 :(得分:0)
您可以优化这段代码:
ProgramHolder pHolder = new ProgramHolder();
v = inflator.inflate(R.layout.programs, null);
pHolder.programSaati = (TextView) v.findViewById(R.id.psaati);
pHolder.programAdi = (TextView)v.findViewById(R.id.pname);
pHolder.programAdi.setText(p.getProgramAdi());
pHolder.programSaati.setText(p.getProgramBaslangic());
v.setTag(pHolder);
viewHolder.l.addView(v);
作为View Holder
模式,您应该存储这些布局,也可以存储在静态变量中:
static ProgramHolder pHolder;
if (pHolder == null) {
View v = new View(context);
v = inflator.inflate(R.layout.programs, null);
pHolder.programSaati = (TextView) v.findViewById(R.id.psaati);
pHolder.programAdi = (TextView)v.findViewById(R.id.pname);
}
这里的主要目的是减少使用时间findViewById()
。之后,在循环中,您可以创建自定义视图,并根据pHolder
分配每个字段。
希望这有帮助:)