yildirimyigit
yildirimyigit

Reputation: 3022

Animating a specific row in the listview

I have a listView in my application and at some point during the execution, I want to highlight a specific row. I found some examples but I keep getting errors. Here is my code:

public void animate(int pos){ //pos:position of the row in the list
    lv1=(ListView)findViewById(R.id.listView1);
    Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.scale);
    ((View)lv1.getItemAtPosition(pos)).startAnimation(animation);
}

and scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
</set>

If anyone can help with this I would really appreciate it.

EDIT: I want to give an additional information I just found out. I get a NullPointerException when I run the code. Following line causes the exception

((View)lv1.getItemAtPosition(pos))

I cannot get the items in listView. I changed my program as follows:

public void animate(int pos){ //pos:position of the row in the list
    lv1=(ListView)findViewById(R.id.listView1);
    View v=lv1.getChildAt(pos);
    Toast.makeText(getBaseContext(),Boolean.toString((v==null)),Toast.LENGTH_SHORT).show();
}

It always displays "true". I didn't understand where the problem is stemming from.

Upvotes: 2

Views: 1188

Answers (1)

rwarner
rwarner

Reputation: 597

I'm not sure if this helps you at all, but I found your code useful to me to solve my problem. I got the same type of effect working with the following code:

//Animation swipe to the right
View view = lv.getChildAt(pos);
Animation animation = AnimationUtils.loadAnimation(TaskMain.this, R.anim.slideright);
view.startAnimation(animation);

Then I just put that in my area when I want to activate it. For myself in particular I activated on gesture.

Upvotes: 1

Related Questions