Adding dynamically textviews to RecyclerView ViewHolder mess up the views

Adding dynamically textviews to RecyclerView ViewHolder mess up the views

android recyclerview add item dynamically
how to add a dynamic view in between items of recyclerview in android
dynamically add multiple textview to linearlayout android
dependencies for recyclerview and cardview in android
how to add linearlayout dynamically in android
how to import recyclerview in android studio
custom recyclerview android example
how to create dynamic recyclerview android

Im trying to implement dynamically textview, into my view holder, on the first scroll everything goes well, the cards and so are the properties for each, but if if i scroll back up again the textview start to pop where they shouldn't.

my custom adapter:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClick(MyProduct product);

int counter = 0;
private OnItemClickListener listener;
private Context context;
List<MyProduct> myProducts;

public CustomAdapter(OnItemClickListener listener, List<MyProduct> myProducts, Context context)
    this.listener = listener;
    this.myProducts = myProducts;
    this.context = context;

public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.product_list, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;

public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) {

    if( myProducts != null) {

        holder.bind(myProducts.get(position), listener);

public int getItemCount() {
    return myProducts.size();

public class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public TextView textViewProductTitle;
    public TextView textViewProductDescription;
    public TextView textViewProductSerialNumber;
    public TextView textViewProductPrice;
    List<TextView> textViews;
    ViewGroup viewGroup;

    //Initializing Views
    public ViewHolder(View itemView) {

        viewGroup = (ViewGroup)itemView.findViewById(;
        textViews = new LinkedList<>();
        imageView = (ImageView) itemView.findViewById(;
        textViewProductTitle = (TextView) itemView.findViewById(;
        textViewProductDescription = (TextView) itemView.findViewById(;
        textViewProductSerialNumber = (TextView) itemView.findViewById(;
        textViewProductPrice = (TextView) itemView.findViewById(;


    public void bind(final MyProduct item, final OnItemClickListener listener) {


        if(counter< myProducts.size())
            for (int i = 0; i < item.getBranchCounter(); i++) {
                TextView textView = new TextView(context);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

any advice would be helpful, ty!

Adding views dynamically into a recycler view is a BAD idea. THe point of a recycler view is it recycles the same view structure over multiple items. So when it recycles, you've suddenly added that view to a new item that doesn't need it. Not having dynamic views is almost a pre-requisite of having a recycler view.

A better way is to play with visibilities. Put the text view in all views, but make it GONE. In your bind, you'd then set the visibility of the text view for every item- either to GONE or VISIBLE. THis will get you the effect you want.

Another way would be by using different view types for with and without the extra view, but that tends to be more complicated.

RecyclerView adapter with multiple view items, But things get really messy when this size of the list grows and effs up the user If the screen has the size to fit 5 such contact cards, recycler view These views 1 and 2 are then used to repopulate with the data and added back to the ViewHolder {; private TextView txtName;; private TextView txtNumber;� Recyclerview is works just like listview but more well organised, felexible to customize and optimize to work and in rendering the view of large data set. so in this tutorial we will see how to add items dynamically in recyclerview.for that first, i have created recycler view in which items are added and removed dynamically. in this app we can add items in recyclerview and when we click on the

I completely agree with @Gabe Sechan. But even if you are interested in adding textView dynamically. Then you can declare an integer at the top of adapter class like

int repeat=0;

Now use variable to count each textviews you have added. Also check whether it exceeds the size of your array.

if (repeat <= yourArray.size()) {
    for (int i = 0; i < yourArray.size(); i++) {
        TextView valueTV = new TextView(getApplicationContext());
        valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

This will limit the duplication of the textview.

Android working with RecyclerView, Open build.gradle and add recycler view dependency. ViewHolder { public TextView title, year, genre; public MyViewHolder(View view) { super(view); title position say 10th when i scroll up it messed with some other position's data here is the I have a List that i want to fill dynamically from a database. Android RecyclerView Multiple ViewType Project Structure. We’ll be implementing three view types (text, image, audio) that are inflated by three different layouts. Each has its own implementation specified in the adapter class. Code. Our activity_main.xml contains the CoordinatorLayout as the root and the RecyclerView acts as it’s child view.

Add linear layout, inside linear layout add dynamic textview then, above loop add below code,

 if (ll_layout.getChildCount() > 0)

we solved our issue adding this code

Android RecyclerView - Simple List, the ViewHolder pattern and have made is more difficult to mess up. There is nothing special about adding a RecyclerView in your xml layout: Instead of working with a view containing a ViewHolder we now have a ViewHolder containing a view ViewHolder { private final TextView mTextView; public� RecyclerView and a few other elements are a part of an external library so we must add this external library called Support Library. Support Library is a library for components and other very helpful classes, provided by Google, that enables us to work with modern components, and components that were added later in Android Development.

RecyclerView not returning correct number of lines in TextView , In my RecyclerView adapter, I'm using getLineCount() on a TextView to return the Custom dynamically-generated compound View recreated in RecyclerView, you could match the view type with the supporting data (for the addition of the onBindViewHolder() is doing something intensive like messing with Bitmaps. RecyclerView.ViewHolder. The ViewHolder is an abstract class that we also extend from RecyclerView. The ViewHolder provides us with common methods to help us reference a View we have placed on the RecyclerView even after the Recycling machinery in the RecyclerView has changed various references we don't know about. Large Lists

Android Layout and Views dynamically, Android Layout and Views dynamically - android. ViewHolder{ public ImageView productImage; public TextView color; public RecyclerView - use adapter in LinearLayouts (or use the RecyclerView with a corresponding LayoutManager) If you do want to add all the items from your adapter in a linearlayout, I suppose� public ViewHolder(View v) Otherwise the whole recyclerview gets messed up! and where i can add/remove/edit items dynamically but the problem is after using

Build complex screens faster on Android — Our journey with Epoxy , RecyclerView is such an important UI component on Android. So our awesome design team came up with this beautiful intuitive design : on the UI is based on how you add the model to the adapter or controller. These models are mapped to your custom views or xml layouts. textView.text = text it must work with RecyclerView without implementing a new adapter; I must be able to reuse cells in another RecyclerView; I must be able to easily add other cell types into RecyclerView. Implementation. First I’ve taken a look at things I always do within the adapter, to do that I created a test implementation and analyzed the methods that I