简体   繁体   中英

How to create programmatically textview in RecyclerView.ViewHolder

Trying to figure out how to add dynamically changing number of textviews to my viewholder.

Im getting (int) counter from my server, which represent the number of proprties in the viewholder. each viewholder have different number of properties.

How to implement this?

In your ViewHolder store the new TextViews in a List, and add each TextView to the cell's itemView in the ViewHolder. Should look similar to the code below:

ViewGroup viewGroup = (ViewGroup)itemView.findViewById(R.id.your_viewgroups_id); //Usually a LinearLayout or RelativeLayout
List<TextView> textViews = new LinkedList<>();
for (int i = 0; i < count; i++) {
     TextView textView = new TextView(context)
     textViews.add(textView);

     viewGroup.addView(textView);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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