简体   繁体   English

将不同数量的TextViews添加到ArrayAdapter中的LinearLayout持有人

[英]Add different number of TextViews to LinearLayout holder in ArrayAdapter

I have a listview with CustomArrayAdapter, 我有一个CustomArrayAdapter的列表视图,

want to add dynamically different amount of TextViews to LinearLayout Holder in that listView (like List of TextViews in each row) 想要向该listView中的LinearLayout Holder动态添加不同数量的TextView(例如,每行中的TextView列表)

  @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;

}

在此处输入图片说明

If you know how many types of layout you will need it is possible to use method: 如果您知道需要多少种布局,可以使用method:

getViewTypeCount() 

This method should return total number of unique layouts. 此方法应返回唯一布局的总数。

The next step is creating method: 下一步是创建方法:

getItemViewType(int position)

that will inform which layout to use for given position. 这将告知给定位置要使用哪种布局。

In getView() you need to inflate layout only if it is null and determine layout by getItemViewType(position) 在getView()中,仅当布局为null时才需要对其进行膨胀,并通过getItemViewType(position)确定布局

For details, refer to this tutorial 有关详细信息,请参阅本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM