Android ListView - 添加动态行到行

时间:2015-04-29 07:55:26

标签: android listview

在我的listView我有3种行类型,TXT,IMG,SMS。每个都有不同的行布局,工作正常如下:

... extends BaseAdapter ...

public View getView(int position, View convertView, ViewGroup parent) {

    TextViewHolder textViewHolder = null;
    ImageHolder imageHolder = null;
    SmsHolder smsHolder = null;
    //PlaceHolder placeHolder = null;

    ConversionModel conversion = getItem(position);
    int type = conversion.type;

    if (convertView == null) {
         if(type == ConversionModel.TXT) {
            textViewHolder = new TextViewHolder();

            convertView = mInflater.inflate(R.layout.convers_txt,  null);
            textViewHolder.textView = (TextView)convertView.findViewById(R.id.row_txt);

            convertView.setTag(textViewHolder);
        }

      if(type == ConversionModel.IMG) {
            imageHolder = new ImageHolder();

            convertView = mInflater.inflate(R.layout.convers_img,  null);
            imageHolder.img = (ImageHolder)convertView.findViewById(R.id.row_txt);

            convertView.setTag(textViewHolder);
        }
       ....
    }

   ...

  }

我的目的是:

-alllist-----------
----txt------------
----img------------
-----(multipledata) //placeholder must has dynamic rows in it
--------button-----
--------button-----
---sms-------------
.....

我的placeHolder必须包含动态行,因为我不知道来自服务器的数据数量。我试着嵌套listview,但只有shows first data in the list.

我还将LinearLayout添加到placeHolder并添加了这样的项目(伪):

for data in datas {
   Button btn = new Button(ctx)
   ...
   placeHolder.layout.addView(btn);
} 

但每当我滚动listView时,getView方法就会变得混乱,而且它的渲染时间相差几乎是10-20倍,而它应该是2-3。

我怎样才能做到这一点?我查看了Telegram等应用程序来源,但未找到类似的方法。

解决方案:

String arr[] = conversion.message.split(Pattern.quote("$$"));

 LinearLayout layHolder = new LinearLayout(ctx);

 for (int i = 0; i < arr.length; i++) {
      Button test_btn = new Button(ctx);
      //PlaceModel pm = new PlaceModel(arr[i]);
      layHolder.addView(test_btn);
 }

  placeHolder.linearLay.removeAllViews(); //<- THIS
  placeHolder.linearLay.addView(layHolder);

2 个答案:

答案 0 :(得分:1)

在您的子布局中添加一个线性布局,如下所示:(对于演示,我在奇数子项中添加了一个按钮,在偶数子项位置添加了两个按钮)

    ...    
    <!--main linear layout contnet-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <LinearLayout
        android:id="@+id/layout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

并在其中添加按钮:

在getView()

 ...        
        LinearLayout layout = (LinearLayout)   view.findViewById(R.id.layout);

   //important line
   layout.removeAllViews();

   if (position % 2 == 0)
        {
            Button b1 = new Button(_context);
            b1.setText("B1");
            layout.addView(b1);
        }
        else
        {
            Button b1 = new Button(_context);
            b1.setText("B1");
            layout.addView(b1);
            Button b2 = new Button(_context);
            b2.setText("B2");
            layout.addView(b2);
        }

答案 1 :(得分:0)

在适配器中实现两个方法

UILabel *label = ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
      [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];

 public int getItemViewType(position){
     int type = getItem(position).get type();
     return type ==  ConversionModel.TXT ? 0 : type ==  ConversionModel.IMG ? 1 : 2;
 }

将解决您的问题