1

我正在尝试为我的列表视图使用两种布局。当我滚动列表视图时,布局更改为第一个。编码:

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;
}
4

2 回答 2

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;
}
于 2012-09-13T12:50:54.063 回答
1

如果两个布局仅在颜色上有所不同,则重用 convertView 并调整 elemtns 的颜色(保存您查看膨胀)。

如果视图不同(例如 - 具有不同的图标和元素),则使用根元素的 ID 来确定提供的视图的种类,以确定它是否是合适的类型,只有在不合适的情况下才会膨胀。

还可以考虑使用 ViewHolder 模式来避免查找文本视图:

http://www.jmanzano.es/blog/?p=166

于 2012-09-13T13:10:54.490 回答