Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate item in ListView when being clicked?

I want to have one ListView that when I click on the item the view slide out to the left.

So I have:

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            arg1.startAnimation(openAnimation);
        }
    });

However, animation applied to the different item in the list instead of the one being clicked on. The behavior seems to be random as sometime it happened to more than one item at the same time.

As I suspected this is because of the way Adapter reuse it's view to create item. I went to modify the getView method in my Adapter to inflate new view every time it's being called. Then the animation doesn't happen any more.

Is there a way to resolve this? I tried to move the animation to inside my Adapter but then I can not associate it with other action on the Listview.

Ultimately, I want the item to be clickable but when swipe left/right reveal delete button (iOS delete behavior). Am I on the wrong track here? This should be possible though as Android can implement swipe to remove in the Notification bar.

like image 509
RobGThai Avatar asked Nov 13 '22 07:11

RobGThai


1 Answers

I recommend that you check this thread also.

I don't think, that this is possible without having to modify your adapter to suit this type of behavior. For what I understand, you don't have any problems with implementing the code recognizing the swipe gestures on different ListView-rows, only with the animation the should follow this gesture on according row(s).

I'd rewrite the adapter to suit at least 2 row types: normal rows, and rows to be deleted. In the "getView()" method of your adapter, you should only reuse the convertView of normal Views. Rows that are to be deleted should not reuse them, so that animating one would not modify the others. Upon clicking a normal row, you should first tell the adapter that the row on the clicked position is now of type to-be-deleted, call .notifyDatasetChanged(), and then start the animation on that row.

like image 161
Zsombor Erdődy-Nagy Avatar answered Nov 16 '22 02:11

Zsombor Erdődy-Nagy