2014-02-13 44 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 
    } 

你需要我的取决于你最视图类型覆盖动画方法。