2014-02-17 94 views

回答

0

有沒有這個快速的例子。但你可以做的是保持跟蹤你滾動的方式,並相應地顯示或隱藏視圖

例如獲取ListView的第一個可見位置跟蹤這個,如果它小於你知道你滾動之前這樣你可以顯示視圖。如果它更大,則隱藏視圖。

這是一種簡單的方法,以便您希望更精確地使用onTouchListeners和y運動的座標。

http://developer.android.com/reference/android/widget/ListView.html

+0

感謝您的回答。我會試圖弄明白,如果你找到好榜樣,請讓我知道。你有什麼想法他們如何做選擇視圖動畫?當向下滾動時,選項視圖也會慢慢向下滾動 – NaiveBz

+0

如果您需要更精確地使用onTouchListeners,則可以在顯示或如上所述時執行正常翻譯動畫。 – QVDev

+1

這可能會在這裏回答:http://stackoverflow.com/questions/15464649/android-listview-floating-first-row,你可以從https://code.google.com/p查看示例應用程序/ romannurik-code/source/browse/misc/scrolltricks/ – Robert

4

簡單的解決方案:

public abstract class OnScrollObserver implements AbsListView.OnScrollListener { 

public abstract void onScrollUp(); 

public abstract void onScrollDown(); 

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

int last = 0; 
boolean control = true; 

@Override 
public void onScroll(AbsListView view, int current, int visibles, int total) { 
    if (current < last && !control) { 
     onScrollUp(); 
     control = true; 
    } else if (current > last && control) { 
     onScrollDown(); 
     control = false; 
    } 

    last = current; 
} 

用法:

listView.setOnScrollListener(new OnScrollObserver() { 
     @Override 
     public void onScrollUp() { 

     } 

     @Override 
     public void onScrollDown() { 

     } 
    }); 
+0

這可以應用於線性佈局嗎? – Pihu

+0

Nop,在ScrollView中使用LinearLayout。不要忘記爲ScrollView設置OnScrollObserver。 –

0

這是我自己的實現: 通知:

  • 查看被隱藏應該是固定的高度
  • 我們並不會被Visiblity.GONE
  • 隱藏視圖我們將最終高度設置爲0px

下面是代碼:

//Your view which you would like to animate 
    final RelativeLayout yourViewToHide = (yourViewToHideativeLayout) findViewById(R.id.topWrapper); 
    //The initial height of that view 
    final int initialViewHeight = yourViewToHide.getLayoutParams().height; 
    listView.setOnScrollListener(new AbsListView.OnScrollListener() { 
     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 

     } 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      //Try catch block for NullPointerExceptions 
      try{ 
       //Here is a simple delay. If user scrolls ListView from the top of the screen to the bottom then continue 
       if(firstVisibleItem % visibleItemCount == 0) { 
        //Here we initialize the animator, doesn't matter what values You will type in 
        ValueAnimator animator = ValueAnimator.ofInt(0, 1); 
        //if Scrolling up 
        if (fastScrollSB.getProgress() > view.getFirstVisiblePosition()){ 
         //Getting actual yourViewToHide params 
         ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams(); 
         if (!animator.isRunning()) { 
          //Setting animation from actual value to the initial yourViewToHide height) 
          animator.setIntValues(params.height, initialViewHeight); 
          //Animation duration 
          animator.setDuration(500); 
          //In this listener we update the view 
          animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
           @Override 
           public void onAnimationUpdate(ValueAnimator animation) { 
            ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams(); 
            params.height = (int) animation.getAnimatedValue(); 
            yourViewToHide.setLayoutParams(params); 
           } 
          }); 
          //Starting the animation 
          animator.start(); 

         } 
         System.out.println("Scrolling up!"); 
        //If not scrolling 
        } else if (fastScrollSB.getProgress() == view.getFirstVisiblePosition()) { 

         System.out.println("Not Scrolling!"); 
        //If scrolling down 
        } else if (fastScrollSB.getProgress() < view.getFirstVisiblePosition()){ 
         //Getting actual yourViewToHide params 
         ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams(); 
         if (!animator.isRunning()) { 
          //Setting animation from actual value to the target value (here 0, because we're hiding the view) 
          animator.setIntValues(params.height, 0); 
          //Animation duration 
          animator.setDuration(500); 
          //In this listener we update the view 
          animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
           @Override 
           public void onAnimationUpdate(ValueAnimator animation) { 
            ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams(); 
            params.height = (int) animation.getAnimatedValue(); 
            yourViewToHide.setLayoutParams(params); 
           } 
          }); 
          //Starting the animation 
          animator.start(); 

         } 
         System.out.println("Scrolling down!"); 

        } 
       } 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    });` 

希望它適合你的需求:)

+1

你還沒有添加'fastScrollSB' :) – xAqweRx

相關問題