1

リストビューに2つのレイアウトを使用しようとしています。リストビューをスクロールしているときに、レイアウトが最初のものに変更されます。コード:

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

両方のレイアウトの色のみが異なる場合は、convertViewを再利用して、要素の色を調整します(表示を膨らませて保存します)。

ビューが異なる場合(たとえば、アイコンと要素が異なる場合)、ルート要素のIDを使用して、提供されるビューの種類を判別し、適切なタイプであるかどうかを判断し、そうでない場合にのみ膨張させます。

また、テキストビューのルックアップを回避するために、ViewHolderパターンを使用することを検討してください。

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

于 2012-09-13T13:10:54.490 に答える
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 に答える