1

I currently have a listview that has a header and footer attached. I am now trying to animate the items within the list itself, but when I apply a LayoutAnimationController to to the listview, the header is also animated. Is there a way to apply the animation to the whole list without affecting the header?

I have currently already tried the solution at Can LayoutAnimationController animate only specified Views by making a LinearLayout subclass that checks for animatable, but the header is still animating in with the rest of the items.

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);
Community
  • 1
  • 1
GrouchyPanda
  • 1,004
  • 8
  • 20
  • Is there some gif-video-similar to see what kind of animation are you trying to achieve? – nKn Feb 17 '14 at 21:08
  • Did you had any progress in here? My answer helped somehow? :) – MatheusJardimB Feb 18 '14 at 17:48
  • The animation type shouldnt matter, as any animation would affect the views in the same way. Just for arguments sake though, I am doing an alpha/translate set on the views. And Matheus, my only progress is that I moved the animation into the getView of the wrapped adapter, which works, but not ideal as this then animates all views as you scroll as well, which can be too overwhelming in a list of say 300 results – GrouchyPanda Feb 18 '14 at 18:24
  • BTW, found this really cool listview project - check it out: https://github.com/nhaarman/ListViewAnimations – MatheusJardimB Feb 19 '14 at 16:01
  • Hi did you solve this issue? I am looking for the same thing :) – Furedal Jun 29 '15 at 12:57
  • @Furedal No, unfortunately in all that I looked through, I wasn't able to find a solution for this. The best I managed was what I mentioned in my comment above. Haven't looked into the RecyclerView yet to see if this suffers the same issue, but you might try that path if you're still coming up empty. – GrouchyPanda Jul 06 '15 at 18:00

3 Answers3

1

I had the same problem described above - the animation was being applied to the ListView header as well as list rows. I solved this problem with help from this answer. I subclassed the outermost View of my header (in my case a FrameLayout) and overrode the animation methods to take no action. For example:

list_header.xml:

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

and

package my.project

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

The animation method you need to override my vary depending on the type of your outermost view.

Community
  • 1
  • 1
0

Not really the best solution, but would it be an alternative to for exemple:

  • create a container view (LinearLayout with vertical orientation)
  • add your header view
  • add your ListView
  • add a footer view

I mean, header and footer would be independent of your list component and would be a quick fix. Isn't it? You'd still being able to control header/footer visibility, for example, just by setting visibility as View.GONE.

Also 1, is there any other method you could implement? I mean, canAnimate or something like that? Also 2, how is your getView method implemented.

Just providing a quick example of what I've explained:

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

I believe the problem got solved now :)

MatheusJardimB
  • 3,599
  • 7
  • 46
  • 70
  • The problem with addressing it like this is that you are making the header and footers fixed, but I want them to be able to scroll with the listview, which is why I had them added as a header and footer directly in the listview – GrouchyPanda Feb 18 '14 at 18:19
  • You're right, got it! But can't the external LinearLayout become a ScrollView? Or surround everything with a ScrollView? – MatheusJardimB Feb 18 '14 at 18:21
  • No it cant. If the external layout is a scrollview then the listview will lose its touch events and no longer scroll. Also, I need the ListView's onScollChangeListener, which does not have a comparable method in scrollviews – GrouchyPanda Feb 18 '14 at 18:25
  • Sure, I'll investigate another alternatives. Have you tried the `canAnimate` method I mentioned? – MatheusJardimB Feb 18 '14 at 18:27
  • Yes, I tried to override the canAnimate method in my custom layout but the LayoutAnimationController still animates it. – GrouchyPanda Feb 18 '14 at 21:39
  • Hi @ChrisM, just changed the xml in order to fit your needs. Think now it's solved :D – MatheusJardimB Feb 19 '14 at 13:45
  • No, the change in xml is just showing a solution i told you wouldn't work about 4 comments ago. I cannot use a ScrollView as it does not have access to the onScrollListener that is in the ListView class. Sadly, still not solved – GrouchyPanda Feb 19 '14 at 18:10
0

I try like this if you want just look this xml file,

i declare header and footer separate & Listview separate in this if you want you can Animate listview . header and footer will not get animate.

if you give header and footer Animate then it will also animate...

<?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> 
R KiranKumar
  • 825
  • 1
  • 8
  • 27
  • Your solution has the same problem as @MatheusJardimB. You are making the header and footer static by drawing them in a ViewGroup container with the listview. I however want the header and footers to scroll with the list items. – GrouchyPanda Feb 18 '14 at 18:21