Question

I have a listview with animated listitems, when an item inside the listview is selected, it fades out and becomes invisible, but the issue i have is that it doesn't go back to it's normal state when another item has been selected.

I have set android:fillEnabled="true"andandroid:fillAfter="true"to make the listitem stay animated.

Like this:

enter image description here

  1. ListItem is selected.
  2. ListItem is animated and fades out.
  3. Is fully animated and invisible.
  4. Another ListItem is selected.
  5. Gets animated and fades out.
  6. Both ListItems are faded out, but i just want one at a time to be animated/invisible.

Here is the animation.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/bounce_interpolator"
android:fillEnabled="true"
android:fillAfter="true"
>

<scale
android:duration="600"
android:fromXScale="0.75"
android:fromYScale="0.75"
android:pivotX="25%"
android:pivotY="25%"
android:toXScale="1.00"
android:toYScale="1.00" />

<alpha
android:duration="250"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

</set>

And the listview:

@Override
        public void onComplete(List<Profile> friends) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSpinner.setVisibility(View.GONE);
                    mSpinner.clearAnimation();
                }
                });

            // populate list
            List<String> values = new ArrayList<String>();
            for (Profile profile : friends) {
                //profile.getInstalled();
                values.add(profile.getName());
            }

            listView = (ListView) findViewById(R.id.list);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {

                  Animation anim = AnimationUtils.loadAnimation(SendActivity.this, R.anim.jump);
                  view.startAnimation(anim);

            }
            });  

            ArrayAdapter<String> friendsListAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
            friendsListAdapter.sort(new Comparator<String>() {
                @Override
                public int compare(String lhs, String rhs) {
                    return lhs.compareTo(rhs);    
                }
            });
            mFriendsList.setAdapter(friendsListAdapter);
        }
    };

Any tips on how i could do this?

Was it helpful?

Solution

There are other (more elegant) ways to do that. But this is what came to my mind first + I have already proposed this solution in comment

private View animatedView = null;

//....code .. code .. code...

listView.setOnItemClickListener(new OnItemClickListener() {    
    @Override
    public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) { 
        if (animatedView != null){
           Animation anim = AnimationUtils.loadAnimation(SendActivity.this, R.anim.unjump);
           animatedView.startAnimation(anim);
        }

        Animation anim = AnimationUtils.loadAnimation(SendActivity.this, R.anim.jump);
        view.startAnimation(anim);
        animatedView = view;
    } 
});  



//another animation (called it unjump) to clear the alpha level 
//reusing your stuff
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/bounce_interpolator"
  android:fillEnabled="true"
  android:fillAfter="true"
>

<scale
  android:duration="600"
  android:fromXScale="0.75"
  android:fromYScale="0.75"
  android:pivotX="25%"
  android:pivotY="25%"
  android:toXScale="1.00"
  android:toYScale="1.00" />

<alpha
  android:duration="250"
  android:fromAlpha="0.0"   //exchanged from/to -alpha levels (so this would come from transparent to non-transparent)
  android:toAlpha="1.0" />
</set>

OTHER TIPS

you need devise some logic in your adapter's getView method such that the selected row's backing Object has a Boolean property, something like isClicked. In the getView method check if the object has it as true, and if yes return null else return your view.

You can devise alternate logics similarly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top