我正在尝试为列表视图使用两种布局。当我滚动列表视图时,布局将更改为第一个。 代码:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null) fll = flag.get(position);
if(fll.equals("I")) {
vi = inflater.inflate(R.layout.conersation_replay, null);
TextView t1 = (TextView)vi.findViewById(R.id.mess1);
TextView t2 = (TextView)vi.findViewById(R.id.name1);
TextView t3 = (TextView)vi.findViewById(R.id.time1);
t1.setText(message.get(position));
t2.setText(name);
t3.setText(time.get(position));
}
else {
vi = inflater.inflate(R.layout.conversation_custome, null);
TextView t1 = (TextView)vi.findViewById(R.id.mess);
TextView t2 = (TextView)vi.findViewById(R.id.name);
TextView t3 = (TextView)vi.findViewById(R.id.time);
t1.setText(message.get(position));
t2.setText(name);
t3.setText(time.get(position));
}
return vi;
}
答案 0 :(得分:1)
你需要在布局中制作相同的控件ID,并且如下所示
@Override
public View getView(int position, View convertView, ViewGroup parent) {
chati = mlist.get(position);
if(fll.equals("I")) {
// your layout here
convertView = ((Activity)mcontext).getLayoutInflater().inflate(R.layout.conersation_replay, null);
}else{
convertView = ((Activity)mcontext).getLayoutInflater().inflate(R.layout.conversation_custome, null);
}
TextView t1 = (TextView)convertView.findViewById(R.id.mess);
TextView t2 = (TextView)convertView.findViewById(R.id.name);
TextView t3 = (TextView)convertView.findViewById(R.id.time);
t1.setText(message.get(position));
t2.setText(name);
t3.setText(time.get(position));
return convertView;
}
答案 1 :(得分:1)
如果两种布局仅在颜色上有所不同,则重复使用convertView并调整elemtns的颜色(保存您查看的膨胀)。
如果视图不同(例如 - 具有不同的图标和元素),请使用根元素的ID来确定提供的视图的类型以确定它是否是合适的类型并且仅在不是时才膨胀。
还要考虑使用ViewHolder模式来避免查找文本视图: