2014-02-13 45 views
1

我目前有一個列表視圖,其中包含頁眉和頁腳。我現在試圖爲列表本身內的項目設置動畫,但是當我將LayoutAnimationController應用到列表視圖時,標題也是動畫的。有沒有辦法將動畫應用到整個列表而不影響標題?將動畫添加到標題中而不對標題應用動畫效果

我目前已經在Can LayoutAnimationController animate only specified Views上嘗試過使用LinearLayout子類來檢查動畫,但標題仍然與其餘項目一起動畫。

public class AnimationAverseLinearLayout extends LinearLayout { 
    private boolean mIsAnimatable = true; 

    public AnimationAverseLinearLayout(Context context) { 
     super(context); 
    } 

    public AnimationAverseLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void setAnimatable(boolean isAnimatable) { 
     if(!isAnimatable) 
      clearAnimation(); 
     mIsAnimatable = isAnimatable; 
    }  

    @Override 
    public void startAnimation(Animation animation) { 
     if(mIsAnimatable) { 
      super.startAnimation(animation); 
     } 
    } 
} 

....

//in onCreateView 
ListView listView = (ListView) v.findViewById(android.R.id.list); 
listView.setLayoutAnimation(Animations.animateListView(listView.getContext())); 
header = (com.yardi.hud.inspections.util.AnimationAverseLinearLayout) inflater.inflate(R.layout.fragment_case_search_header, null, false); 
header.setAnimatable(false); 
listView.addHeaderView(header); 
... 
SearchAdapter adapter = new SearchAdapter(results); 
setListAdapter(adapter); 
+0

是否有一些GIF視頻相似,看看你想要實現什麼樣的動畫? – nKn

+0

你在這裏有什麼進展嗎?我的答案有幫助嗎? :) – MatheusJardimB

+0

動畫類型不重要,因爲任何動畫都會以同樣的方式影響視圖。儘管如此,我正在對視圖做一個alpha/translate集。 而Matheus,我唯一的進步就是將動畫移動到了包裝好的適配器的getView中,但它並不理想,因爲這樣做會在您滾動時動畫所有視圖,這在300的列表中可能太過壓倒結果 – GrouchyPanda

回答

0

沒有真正的最佳解決方案,但它是一種用於爲例替代:

  • 創建一個容器視圖(LinearLayout與垂直取向)
  • 添加您的標題視圖
  • 添加您的ListView
  • 添加一個頁腳視圖

我的意思是,頁眉和頁腳是獨立的組件列表,並會速戰速決。不是嗎?您仍然可以控制頁眉/頁腳的可見性,例如,只需將可見性設置爲View.GONE即可。

也1,有沒有其他方法可以實現?我的意思是,canAnimate或類似的東西? 也2,你的getView方法如何實施。

就提供什麼,我已經解釋了一個簡單的例子:

<!-- Scrollable container --> 
<ScrollView 
    android:id="@+id/scrollContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Container view --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/list" 
     android:orientation="vertical"> 

     <!-- Header view --> 
     <TextView 
      android:id="@+id/header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/header_text" /> 

     <!-- LinearLayout acting as ListView --> 
     <LinearLayout 
      android:orientation="vertical" 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <!-- Insert rows into the fake list --> 
      <TextView 
       android:id="@+id/row_item" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/row_item" /> 

     </LinearLayout> 

     <!-- Footer view --> 
     <TextView 
      android:id="@+id/footer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/footer_text" /> 
    </LinearLayout> 
</ScrollView> 

我認爲現在的問題得到有效解決:)

+0

像這樣解決它的問題是,你正在修復頁眉和頁腳,但我希望它們能夠使用listview進行滾動,這就是爲什麼我將它們作爲頁眉和頁腳直接添加到列表視圖中的原因。 – GrouchyPanda

+0

'對了,明白了!但是外部LinearLayout不能成爲ScrollView嗎?或者用ScrollView環繞一切? – MatheusJardimB

+0

不,它不能。如果外部佈局是滾動視圖,則列表視圖將失去其觸摸事件並不再滾動。另外,我需要ListView的onScollChangeListener,它在滾動視圖中沒有類似的方法 – GrouchyPanda

0

我嘗試這樣,如果你只想看看這個XML文件,

我聲明標題和頁腳分開& Listview單獨在這個如果你想你可以動畫列表視圖。頁眉和頁腳不會生成動畫。

如果你給頁眉和頁腳動畫那麼它也將動畫...

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <!-- Header aligned to top --> 
    <RelativeLayout 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="#FC9" 
    android:gravity="center" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="Fixed Header" 
     android:textColor="#000" 
     android:textSize="20sp" /> 
    </RelativeLayout> 

    <!-- Footer aligned to bottom --> 
    <RelativeLayout 
    android:id="@+id/footer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="#FC0" 
    android:gravity="center" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="Fixed Footer" 
     android:textColor="#000" 
     android:textSize="20sp" /> 
    </RelativeLayout> 

    <!-- LIstview Item below header and above footer --> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/footer" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/header" > 

    </ListView> 

</RelativeLayout> 
+0

你的解決方案與@MatheusJardimB有同樣的問題。您正在通過使用listview在ViewGroup容器中繪製頁眉和頁腳來使其靜態。不過,我希望頁眉和頁腳與列表項目一起滾動。 – GrouchyPanda

1

我曾與上述相同的問題 - 動畫是被應用到ListView頭以及列表行。我在this answer的幫助下解決了這個問題。我將我的標題的最外層視圖(在我的情況下是一個FrameLayout)分類,並覆蓋動畫方法以不採取任何行動。例如:

list_header。XML:

<my.project.NonAnimatingFrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    ... 
    > 
<!-- other views in your header --> 
... 
</my.project.NonAnimatingFrameLayout> 

package my.project 

public class NonAnimatingFrameLayout extends FrameLayout { 
    ... 
    @Override 
    public void setAnimation(Animation animation) { 
    // do nothing 
    } 

你需要我的取決於你最視圖類型覆蓋動畫方法。