在ArrayAdapter中向LinearLayout holder添加不同数量的TextView

时间:2016-04-19 11:25:45

标签: android android-layout listview android-linearlayout

我有CustomArrayAdapter的列表视图,

想要动态地将不同数量的TextView添加到该listView中的LinearLayout Holder(如每行中的TextViews列表)

  @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    //String menu_opcja = getItem(position);
    SupleSubClass supleSubClass = getItem(position);


    if (convertView==null){
        convertView = inflater.inflate(R.layout.suplementacja_row, null);
        holder = new ViewHolder();
        holder.suplement = (TextView) convertView.findViewById(R.id.nazwa_suplementu);
        holder.opis = (TextView) convertView.findViewById(R.id.descr);
        holder.linearLayout = (LinearLayout)convertView.findViewById(R.id.linear_layout_test);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.suplement.setText(supleSubClass.getName());
    holder.opis.setText(supleSubClass.getDescr());
    holder.linearLayout.setBackgroundColor(Color.CYAN);


    return convertView;

}


private static class ViewHolder{
    TextView suplement;
    TextView opis;
    LinearLayout linearLayout;

}

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您知道需要多少种类型的布局,可以使用方法:

getViewTypeCount() 

此方法应返回唯一布局的总数。

下一步是创建方法:

getItemViewType(int position)

将告知用于给定位置的布局。

在getView()中,只有当它为null并且通过getItemViewType(position)确定布局时才需要扩展布局

有关详细信息,请参阅this tutorial