【发布时间】: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);
}
但每当我滚动 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);
【问题讨论】:
-
在主要内容(图像或文本)下方添加线性布局,并在该线性布局中添加按钮。
-
感谢您的帮助,现在试试。