How can I animate Deletion /Insertion actions in ListView which is being populated by CursorAdapter & CursorLoader?

50 views Asked by At

I am a beginner in Android development. I am using a ListView in my app which is being populated by SQLite database with the help of CursorAdapter. And I am using CursorLoader with notifyChange.

As of now the ListView updates in real time with new data as I delete or Insert new data into database. But I want to add animation for items for deletion and insertion of items.

I was able to find deletetion animation for when using ArraylistAdapter - DevBytes -> https://www.youtube.com/watch?v=YCHNAi9kJI4 (DevBytes Code)

But how to apply animations when using a CursorAdapter?

1

There are 1 answers

1
Hiteshri Yagnik On

I am also a beginner but for insertion operation, you have to add the animation to the oncreate() method when list is loaded the first time.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    ListView lv = (ListView) findViewById(R.id.listView);
    
    List<ItemDetail> itemList = createItems(50);

    // Load animation
    final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_anim);
    
    // Find ListView to populate
       ListView lv = (ListView) findViewById(R.id.lvItems);
     // Setup cursor adapter using cursor from last step
       TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
    // Attach cursor adapter to the ListView 
       lv.setAdapter(todoAdapter);

    lv.setAdapter(aAdpt);
    
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, final View view, final int position,
                long id) {

            anim.setAnimationListener(new Animation.AnimationListener() {
                
                @Override
                public void onAnimationStart(Animation animation) {
                    
                    view.setHasTransientState(true);
                }
                
                @Override
                public void onAnimationRepeat(Animation animation) {
                    
                    
                }
                
                @Override
                public void onAnimationEnd(Animation animation) {
                    ItemDetail item = aAdpt.getItem(position);
                    aAdpt.remove(item);
                    view.setHasTransientState(false);
                }
            });
            view.startAnimation(anim);
        }
    });
    
}


private List<ItemDetail> createItems(int size) {
    List<ItemDetail> result = new ArrayList<ItemDetail>();
    for (int i=0; i < size; i++) {
        ItemDetail item = new ItemDetail(i, "Item " + i);
        result.add(item);
    }
    
    return result;
}

For the animation you have to create animation file (xyz.xml) under the res/anim folder.