Nico Dioso
Nico Dioso

Reputation: 395

Add a layout between Recycler View rows?

What I want to achieve is if I could detect a string within a recycler view item I would inflate something after it and before the next recycler view item is shown.

Upvotes: 0

Views: 1152

Answers (3)

Mitesh Nandpal
Mitesh Nandpal

Reputation: 26

For example like you have CustomObject class. Take boolean (isCustomView) to add custom layout.

public class YourObject {
String customString ="";
boolean isCustomView=false;

public String getCustomString() {
    return customString;
}

public void setCustomString(String customString) {
    this.customString = customString;
}

public boolean isCustomView() {
    return isCustomView;
}

public void setCustomView(boolean customView) {
    isCustomView = customView;
}

}

Before sending data to Adapter make this changes to your data

 ArrayList<YourObject> yourObjectArrayList = new ArrayList<>();

    // Assume You have yourObjectArrayList with data filled

    // Take temp object 
    YourObject object = new YourObject();
    object.setCustomView(true);

    for (int i = 0; i < yourObjectArrayList.size(); i++) {
        if(yourObjectArrayList.get(i).getCustomString().equals("YOUR CUSTOM STRING")){

            // Adding a custom object before That Position
            if (i==0)
                yourObjectArrayList.add(0,object);
            else
                yourObjectArrayList.add(i-1,object);

            // Adding a custom object After That Position. Note here i+2 is a must
            yourObjectArrayList.add(i+2,object);
        }
    }
    // Add data to RecyclerView adapter & notify

Override getItemViewType() in recycler adapter

@Override
public int getItemViewType(int position) {
    return (yourObjectArrayList.get(position).isCustomView()) ? R.layout.your_custom_layout : R.layout.your_raw_layout;
}

Make below changes to override method onCreateViewHolder

 @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;


    if(viewType == R.layout.your_custom_layout){
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_custom_layout, parent, false);
    }
    else {
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_raw_layout, parent, false);
    }

    return new ViewHolder(itemView);

}

Upvotes: 1

Anindya Pal
Anindya Pal

Reputation: 76

holder.dynamicLinearLayout.removeAllViews()
View view = LayoutInflater.from(context).inflate(R.layout.new_layout,null);
holder.dynamicLinearLayout.addView(view);

Try this on onBind

Upvotes: 0

Geet Choubey
Geet Choubey

Reputation: 1077

you can use the int getItemViewType(int); override method of the RecyclerView.Adapter<> class.

@Override
public int getItemViewType(int position){
    if (data.get(position).contains("Your string"))
        return 007; // any random integer you can use
    return super.getItemViewType(position);
}

And in the onCreateViewHolder method

@Override
public void onCreateViewHolder(ViewGroup parent, int viewType){
    if (viewType == 007){ // the view type you returned
        return #special_view // inflate your other layout here which you want to inflate if it contains your special string.
    else 
        return #normal_view;// inflate the normal view here and return
    }
}

Upvotes: 1

Related Questions