Android sticky header RecyclerView/ Section Header RecyclerView

前端 未结 2 1775
野趣味
野趣味 2021-02-08 17:54

Hello All I have just created a demo to work with new android L widget RecyclerView.I have also implemented Pull-To-Refresh using SwipeRefreshLayout but problem for me now is ho

2条回答
  •  你的背包
    2021-02-08 18:07

    public void onScrolled(int dx, int dy)

    those are the two arguments that you're receiving onScrolled, that means, the number of pixels that the RecyclerView changed on the X and Y axis... so probably all you want to do is:

    @Override
    public void onScrolled(int dx, int dy) {
       if(dx < 0) // going up
          showSitckyHeader();
    }
    

    you can probably further improve this implementation by adding a minimum scroll amount. Something like:

    int totalScrolled = 0;
    @Override
    public void onScrolled(int dx, int dy) {
       totalScrolled += dx;
       if(totalScrolled < MIN_SCROLL)
          showSitckyHeader();
       if(dx > 0)
          totalScrolled = 0;
    }
    
    @Override
    public void onScrollStateChanged(int newState) {
       if(newState == SCROLL_STATE_IDLE || newState = SCROLL_STATE_SETTLING)
           totalScrolled = 0;
    }
    

    or even go further and implement speed, counting time, but those types of implementation are more tricky, and you have to test it yourself.

提交回复
热议问题