1

How do I add an item to a RecyclerView at an index that currently has an item and retain any items that exist below it in stacking order? For example, if I have this list:

item = 0, item 1, item 2, ...

I want to insert an item into an existing index but without replacing the original value:

item = 0, item A, item 1, item 2, ...

The code I have used so far is:

public void addRecyclerObject(int position, Object recyclerObject) {
   recyclerObjects.add(position, recyclerObject);
   notifyItemInserted(position);
}

However, when I use execute the above code, the RecyclerView renders as such:

item = 0, item A, item 2, ...

item 1 is completely removed and replaced with another value. How can I insert another item without replacing an existing value? Any suggestions?

1
  • Are you using a List to generate the items within the RecyclerView? Commented Jul 4, 2016 at 1:44

1 Answer 1

0

If you are using a List to intialise the items in your RecyclerView, you can easily insert it into an existing index and the original item stored in that location is moved without being replaced. For example:

list.add(1, "String 1")
list.add(2, "String 2")
list.add(2, "String 3")

String1 is inserted normally. String2 is inserted into the second position. String3 is also inserted into the second position, pushing String2 into the third position and altering the rest of the data set as well. If you need more help, look at this post. Tell me how you go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.