2014-05-04 89 views
0

我有一個列表視圖與動畫listitems,當一個項目內的列表視圖被選中,它淡出,變得不可見,但我有的問題是,它不會回到它的正常狀態當另一個項目被選中時。返回動畫listitem到正常狀態

我已經設置了android:fillEnabled="true"android:fillAfter="true"以使listitem保持動畫狀態。

像這樣:

enter image description here

  1. 列表項被選擇。
  2. ListItem被動畫並淡出。
  3. 完全動畫和隱形。
  4. 選擇了另一個ListItem。
  5. 獲取動畫並淡出。
  6. 這兩個ListItems都會淡出,但我只希望每次都有一個動畫/不可見。

這裏是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> 

和列表視圖:

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

我如何能做到這一點有什麼建議?

+1

店先前所選的元素位置,再按下另一個元素時,返回上一個正常的狀態 –

+0

感謝你能不能表現出我如何添加這個代碼示例? –

回答

1

還有其他(更優雅的)方式來做到這一點。但是,這是來到我的腦海中首先+我已經提出在評論這一解決方案

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> 
+0

感謝非常明確的答案! –

0

您需要在適配器的getView方法中設計一些邏輯,以便所選行的後備對象具有布爾屬性,類似於isClicked。在getView方法中檢查對象是否具有true,如果是,則返回null否則返回您的視圖。

您可以類似地設計替代邏輯。

相關問題