Skip to content

仿商城等滑动至最顶端时,顶部TAB悬浮、固定

Notifications You must be signed in to change notification settings

PuppetZ/ScrollingTricks

Repository files navigation

ScrollingTricks

仿商城等滑动至最顶端时,顶部TAB悬浮、固定

示例

  • 单一TAB顶部悬浮、固定

    • 实现原理:布局中有2个Tab,一个Tab一直处于顶部,另一个在ScrollView中。当滑动时,处于顶部的Tab显示,ScrollView中的隐藏;否则反之。
    • 实现代码:需要重写ScrollView,添加一个滚动监听器
    public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
          if (topView != null) {
              if (y > topView.getHeight()) {//Y轴上滑位移大于顶部tab高度时
                  floatView.setVisibility(View.VISIBLE);
              }else {
                  floatView.setVisibility(View.GONE);
              }
          }
      }

  • 多组Tab顶部悬浮、固定

    • 实现原理:这里使用的是RecyclerView,原理同上个。
    • 实现代码:RecyclerView滚动监听
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                  super.onScrolled(recyclerView, dx, dy);
    
                  curPosition = mLayoutManager.findFirstVisibleItemPosition();
                  mTextView.setY(0);//初始时 tab处于悬浮状态
                  if (mAdapter.getItemViewType(curPosition + 1) == RecyclerViewAdapter.TYPE_GROUP) {
                      View viewByPosition = mLayoutManager.findViewByPosition(curPosition + 1);
                      if (viewByPosition.getTop() > mTextView.getHeight()) {//判断tab悬浮时的条件
                          mTextView.setY(0);
                      } else {
                          mTextView.setY(-(-(viewByPosition.getTop()) + mTextView.getHeight()));
                      }
                  }
                  int j = (curPosition + 1) / 5;//除以5是因为5个一组,实际具体情况具体分析
                  mTextView.setText("第" + j + "组");//第一个group
                  mTextView.setVisibility(View.VISIBLE);
                  View viewByPosition = mLayoutManager.findViewByPosition(curPosition + 1 + 5);//下一个group
                  if (viewByPosition.getHeight() < mTextView.getHeight()) {
    
                      int i = (curPosition + 5 + 1) / 5;
                      mTextView.setText("第" + i + "组");
                  }
    }

 

备注

 因个人知识有限,如有什么意见或建议,欢迎提出,共同进步!

About

仿商城等滑动至最顶端时,顶部TAB悬浮、固定

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages