Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecylerView not animating insert, however it does animate removal

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?

like image 268
Max McKinney Avatar asked Nov 20 '14 16:11

Max McKinney


1 Answers

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);
}
like image 133
yigit Avatar answered Sep 19 '22 22:09

yigit