2017-04-21 54 views
-1

我要顯示和隱藏按鈕時recyclerview向上和向下滾動一段距離(按鈕Distnce高度),我已經寫了一些代碼,但它並不如預期的工作如何計算距離上下滾動回收視圖

@Override 
public void onScrolled(RecyclerView view, int dx, int dy) { 
    super.onScrolled(view, dx, dy); 
    if (verticalOffset > floatingActionMenu.getHeight() && dy > 0) { 
     floatingActionMenu.setVisibility(View.VISIBLE); 
    } else { 
     floatingActionMenu.setVisibility(View.GONE); 
    } 
    verticalOffset += dy; 
} 
+0

如果用戶正在向上滾動並且如果用戶向下滾動,則想要顯示按鈕。你想要這個嗎? –

+0

是的,我想要的東西 – apk

+0

你有解決方案嗎? –

回答

0

也嘗試實現onScrollStateChange。

我的草案看起來像這樣。

int distance =0; 
    int scrollDirection; 

    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     super.onScrolled(recyclerView, dx, dy); 
     scrollDirection = dy; 
     distance+=dy; 
     Log.d("TAG", "onScrolled: " + dy); 
    } 

    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     if (newState == RecyclerView.SCROLL_STATE_IDLE) { 
      if (scrollDirection > 0) { 
       Log.d("TAG", "onScrollStateChanged: " + " STOPPED " + distance); 
       distance = 0; 
      } else { 
       Log.d("TAG", "onScrollStateChanged: " + " STOPPED " + distance); 
       distance = 0; 
      } 
     } 
    } 

根據最終distance值你onScrollStateChange讓你可以deside,隱藏你的按鈕或沒有。我應該告訴你,你還必須在onScrollStateChanged中隱藏/顯示你的FAB嗎?