2Stoned 2Stoned - 4 months ago 20
Android Question

How to create programmaticlly 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.

Any idea how to implement this?

Answer

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);
}