package fr.masciulli.drinks.fragment;

import android.animation.TimeInterpolator;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;

import butterknife.ButterKnife;
import fr.masciulli.drinks.R;

public class DrinksOnScrollListener implements AbsListView.OnScrollListener {
    private static final long DRINKNAME_ANIM_DURATION = 600;

    public static final int NAMEVIEW_POSITION_TOP = 0;
    public static final int NAMEVIEW_POSITION_BOTTOM = 1;

    private final AbsListView mListView;

    private int mPreviousFirstVisibleItem = -1;
    private int mPreviousLastVisibleItem = -1;
    private TimeInterpolator mDecelerateInterpolator = new DecelerateInterpolator();

    private int mNameViewPosition = NAMEVIEW_POSITION_BOTTOM;

    public DrinksOnScrollListener(AbsListView listView) {
        mListView = listView;
    }

    public DrinksOnScrollListener(AbsListView listView, int nameViewPosition) {
        mListView = listView;
        if (nameViewPosition == NAMEVIEW_POSITION_TOP) mNameViewPosition = nameViewPosition;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (totalItemCount == 0) return;

        int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;

        if (firstVisibleItem < mPreviousFirstVisibleItem) {
            // first visible item index has decreased : we are scrolling up
            View root = mListView.getChildAt(0);
            View nameView = ButterKnife.findById(root, R.id.name);
            if (nameView == null) return;
            if (mNameViewPosition == NAMEVIEW_POSITION_BOTTOM) {
                nameView.setTranslationX(nameView.getWidth());
            } else {
                nameView.setTranslationX(0 - nameView.getWidth());
            }
            nameView.animate().translationX(0).setDuration(DRINKNAME_ANIM_DURATION).setInterpolator(mDecelerateInterpolator);
        }

        if (lastVisibleItem > mPreviousLastVisibleItem) {
            // first visible item index has decreased : we are scrolling down
            View root = mListView.getChildAt(visibleItemCount - 1);
            View nameView = ButterKnife.findById(root, R.id.name);
            if (nameView == null) return;
            if (mNameViewPosition == NAMEVIEW_POSITION_BOTTOM) {
                nameView.setTranslationX(nameView.getWidth());
            } else {
                nameView.setTranslationX(0 - nameView.getWidth());
            }
            nameView.animate().translationX(0).setDuration(DRINKNAME_ANIM_DURATION).setInterpolator(mDecelerateInterpolator);
        }

        mPreviousFirstVisibleItem = firstVisibleItem;
        mPreviousLastVisibleItem = lastVisibleItem;
    }
}

@@ -189,16 +189,48 @@
 istView;
+%0Aimport android.widget.ListView;
 %0A%0Aimport
@@ -558,19 +558,16 @@
 e final 
-Abs
 ListView
@@ -849,35 +849,32 @@
 nScrollListener(
-Abs
 ListView listVie
@@ -949,19 +949,16 @@
 istener(
-Abs
 ListView
@@ -1710,32 +1710,34 @@
 e);%0A            
+//
 if (nameView == 
@@ -1742,32 +1742,72 @@
 = null) return;%0A
+            if (nameView != null) %7B%0A    
             if (
@@ -1863,32 +1863,36 @@
                 
+    
 nameView.setTran
@@ -1926,32 +1926,36 @@
 ));%0A            
+ 
+   
 %7D else %7B%0A       
@@ -1939,32 +1939,36 @@
        %7D else %7B%0A
+    
                 
@@ -2022,34 +2022,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
             name
@@ -2158,32 +2158,46 @@
 eInterpolator);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2483,33 +2483,33 @@
 f (nameView 
-=
+!
 = null) 
 return;%0A    
@@ -2492,32 +2492,30 @@
 ew != null) 
-return;%0A
+%7B%0A    
             
@@ -2575,32 +2575,36 @@
                 
+   
+ 
 nameView.setTran
@@ -2638,32 +2638,36 @@
 ));%0A            
+    
 %7D else %7B%0A       
@@ -2667,32 +2667,36 @@
                 
+   
+ 
 nameView.setTran
@@ -2734,34 +2734,41 @@
 ));%0A            
-%7D%0A
+    %7D%0A   
             name
@@ -2755,32 +2755,33 @@
 %0A               
+ 
 nameView.animate
@@ -2870,32 +2870,46 @@
 eInterpolator);%0A
+            %7D%0A
         %7D%0A%0A     

Fix scroll animation in liquor détail