0

I encountered the following bug : I implemented a ListFragment that displays the content of a table. When I long-click an item, I want to delete the corresponding line in the table and of course to remove the item from the list. To do so, I implemented a setOnItemLongClickListener. It works fine. Now I wanted to make it good-looking, so I added in the listener an animation on the view.

view.animate().setDuration(100).alpha(0)

The result is : when I longclick an item, the one below the one I removed also disappear. I guess the animation is also applied on it. This is a shame. Any idea to solve it or any know-bug related to that ?

Please note I also tried to do it through a Runnable() as below but it doesn't work too

view.animate().setDuration(200).alpha(0).withEndAction(new Runnable() { ... }

You can find my code below :

public class NoteListFragment extends ListFragment {

private NoteListInterface INoteList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    this.getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            INoteList.onEdit(position);             
        }
    });

    this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {

            // /!\ RESOLVE ISSUE with animation /!\
            view.animate().setDuration(100).alpha(0);
            return INoteList.onDelete(position);

            /*final int pos = position;
            view.animate().setDuration(200).alpha(0).withEndAction(new Runnable() {

                @Override
                public void run() {

                    try
                    {
                        INoteList.onDelete(pos);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }

                }
            });
            return true;*/
        }
    });
}
MrFlo
  • 309
  • 1
  • 5
  • 14

1 Answers1

1

The reason another item in the list is also animated is most likely view recycling that occurs in a ListView. We use ListView because it recycles views and therefor we can have a 1000 item list with no problems.

You set that view's alpha to 0, but at the same time you remove the item from the adapter and notify the list about it via notifyDataSetChanged or something similar.
At this point the ListView refreshes and removes your item, but the view still has the animation on it. The view is now recycled and used for a different item of your data.

Long story short, removing things with animation in ListView is often a dirty job. This is what RecyclerView is way better at. You should consider using it.
If you still want to go with a ListView, here is a good place to start learning how to animte item removal.

Community
  • 1
  • 1
Andrei Tudor Diaconu
  • 2,157
  • 21
  • 26
  • some things sound good in your link, I'll try asap and let you know. I'll also check the RecyclerView. Thanks for your answer ! – MrFlo Aug 10 '15 at 08:23
  • Honestly, RecyclerView is the way to go. It's newer and it has built in support for animating items. The default behavior is actually to fade out elements on delete and fade them in when adding :) so it's exactly what you need. It's not part of core Android though, it's part of the support library. [Here is a link](https://developer.android.com/training/material/lists-cards.html) to a short Google tutorial for it with cards and [here](https://www.youtube.com/watch?v=MHqpR3yLNfk) is a video about animations in RecyclerView. – Andrei Tudor Diaconu Aug 10 '15 at 08:30
  • One more question then : is it possible to implement RecyclerView with ListFragment ? – MrFlo Aug 10 '15 at 10:21
  • No. But you can use a normal fragment with a layout that has a RecyclerView in it. ListFragment doesn't offer that many advantages anyway. – Andrei Tudor Diaconu Aug 10 '15 at 11:00
  • Hey thanks for your tips, it works great after a couple of hours working on it :) – MrFlo Aug 10 '15 at 15:02