6

So when I add an item it does not animate it in, nor does it scroll to the correct position. However it will animate the object removal when I delete something.

Here is the add code:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == NEWITEMREQUESTCODE && resultCode == Activity.RESULT_OK) { // AFTER CREATING A NEW ITEM OilChangeableItem oilChangeableItem = data.getParcelableExtra("oil_item"); // Add the new item and notify the dataset. oilChangeableAdapter.notifyItemInserted(0); items.add(0, oilChangeableItem); } else if (requestCode == EDITITEMRESULTCODE && resultCode == Activity.RESULT_OK) { // AFTER EDITING AN ITEM OilChangeableItem oilChangeableItem = data.getParcelableExtra("oil_item"); // Replace the existing item at whatever position it was originally at and notify the dataset. oilChangeableAdapter.notifyItemChanged(data.getIntExtra("item_position", 0)); items.set(data.getIntExtra("item_position", 0), oilChangeableItem); } } 

And here is the removal code:

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.dialog_delete_title); builder.setIcon(R.drawable.ic_action_warning); builder.setMessage(R.string.dialog_delete_message); builder.setPositiveButton(R.string.button_text_delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Removed the selected item oilChangeableAdapter.notifyItemRemoved(oilChangeableAdapter.getSelectedViewPosition()); items.remove(oilChangeableAdapter.getSelectedViewPosition()); // Notify the user it has been removed. Toast.makeText(getActivity(), R.string.toast_item_deleted, Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton(R.string.button_text_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create(); builder.show(); mode.finish(); 

Does anyone have any ideas?

2
  • Why do you call notifyItemInserted before actually insert the item? Commented Nov 20, 2014 at 17:57
  • @rciovati I've tried it afterwards and it still doesn't work :P Commented Nov 20, 2014 at 18:00

1 Answer 1

9

It is because you are adding the item "above" the currently visible items.

I understand this looks unexpected if you think about the first position but if RV was showing items from 5 to 10 and a new item is added at position 2, you really don't want your list to move to 4 to 9.

So LinearLayoutManager keeps this simple, if an item is added above or below the list, it does not move around (so that UX is consistent).

If you want it to show position 0 when you add an item to position 0, call recyclerView.scrollToPosition(0) after adding the item. I think what you really want is:

items.add(0, oilChangeableItem); oilChangeableAdapter.notifyItemInserted(0); if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0) { layoutManager.scrollToPosition(0); } 
Sign up to request clarification or add additional context in comments.

10 Comments

So it will scroll to the item, but it still won't animated the item fading in like it does when you remove an item.
Hmm it should if you are calling scroll to position. What are you seeing? İt just jumps and don't animate any view?
Yeah it'll just jump to it. Or if I'm just adding an item for the first time, it'll just pop up and won't fade in.
That's weird. You have not set item animator to null right? Default item animator does not work on gingerbread and animations are disabled in battery saving mode. If you can upload a sample project, I'm happy to look at it
:/, it is happening because the code in question runs in onActivityResult. RV does not run animations in the first layout pass after being attached because it looks weird to fade in everything :/. As a workaround, you can postpone adding the item to the next frame using a handler. 10 ms would do it. developer.android.com/reference/android/os/…, long)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.