2011-12-16 26 views
2

我在應用程序中有一個listView,並且在執行過程中的某個點上,我想突出顯示特定的行。我找到了一些例子,但我一直在收到錯誤。這裏是我的代碼:動畫列表視圖中的特定行

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); 
} 

和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> 

如果任何人都可以用這個,我真的很感激幫助。

編輯:我想提供一些我剛發現的信息。當我運行代碼時,我得到一個NullPointerException。以下行導致異常

((View)lv1.getItemAtPosition(pos)) 

我無法獲取listView中的項目。我改變了我的程序如下:

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(); 
} 

它總是顯示「真」。我不明白問題源於何處。

+1

你得到了什麼錯誤,哪些不起作用? – 2011-12-16 12:24:00

回答

0

我不確定這是否可以幫助你,但我發現你的代碼對我有用,以解決我的問題。我用下面的代碼工作的同類型的效果:

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

然後我把在我區時,我想將其激活。特別是對於我自己,我是以手勢激活的。