【问题标题】:Android ListView - add dynamic row to rowAndroid ListView - 将动态行添加到行
【发布时间】:2015-04-29 20:07:38
【问题描述】:

在我的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 中必须包含动态行,因为我不知道来自服务器的数据数量。我试图做它嵌套列表视图,但它只有shows first data in the list.

我还把LinearLayout 放在placeHolder 上,并添加了这样的项目(伪):

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

但每当我滚动 listViewgetView 方法时,它会呈现相同的按钮几乎 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);

【问题讨论】:

  • 在主要内容(图像或文本)下方添加线性布局,并在该线性布局中添加按钮。
  • 感谢您的帮助,现在试试。

标签: android listview


【解决方案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);
        }

【讨论】:

  • 我试过这个解决方案。当我向下滚动时,按钮添加了 10-20 次,而不是 2-3 次。正如我所说,getView 方法与此解决方案相混淆。
  • 对不起,我正在尝试扩展列表,但我更新了列表视图,它在视图中正确添加了按钮。如果您愿意,我可以发布屏幕截图。
【解决方案2】:

在你的适配器中实现两个方法

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

public int getViewTypeCount(){
       return 3;
  }

会解决你的问题

【讨论】:

  • 我已经在检查类型了。无论如何,现在我正在尝试切换 ExpandableListView。
  • 您在 getView() 中检查视图类型,适配器负责膨胀视图,这就是为什么在任何视图类型中添加子视图时您会遇到问题。这些方法告诉适配器您正在使用多个视图。
  • 正如我所说,我已经检查了“类型”,但您的解决方案没有帮助。但是谢谢哈里斯。
猜你喜欢
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多