【问题标题】:Hide fab button with ListView when scrolling down [duplicate]向下滚动时使用ListView隐藏fab按钮[重复]
【发布时间】:2018-03-28 21:53:22
【问题描述】:

如何在滚动 listView 时隐藏 fab 按钮?

我目前正在使用此代码,但每当我触摸屏幕和滚动时它会隐藏 FAB 按钮,我需要它在滚动时隐藏 FAB 按钮向下和向上滚动时必须再次显示

当前代码:

mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
                floatingActionButton.hide();
            }else{
                floatingActionButton.show();
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });

【问题讨论】:

标签: android listview floating-action-button


【解决方案1】:

试试这个你应该使用onScroll而不是onScrollStateChanged

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) {

        // add here your logic like this
        // int lastItem = firstVisibleItem + visibleItemCount;
        if (firstVisibleItem < 2) {

            floatingActionButton.setVisibility(View.INVISIBLE);
        }else {
            floatingActionButton.setVisibility(View.VISIBLE);
        }
    }
});

【讨论】:

  • 你在哪里使用 int lastItem?
  • 这是一个逻辑部分我的朋友你可以忽略那行
  • 我认为最好使用hideshow。我这样做了:if (lastItem == totalItemCount) floatingActionButton.hide(); 隐藏滚动末尾的按钮。
猜你喜欢
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 2015-02-15
  • 2018-02-26
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多