在ListView上向上滚动时显示ActionBar

时间:2014-08-28 12:48:49

标签: java android android-listview onscrolllistener

我有ListView,当我向下或向上滚动时,我可以使用此解决方案检测到这一点:

listView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) { }

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

        if(listView.getFirstVisiblePosition() == 0)
            getActionBar().show();
        else
            getActionBar().hide();
    }
});

这种方式有效,ActionBar会在滚动回到列表的第一项时显示。当我开始在列表中向上滚动时,我还想再次显示ActionBar

编辑:

这是最后一种方式,并不总是有效..有时动作栏会消失,永远不会回来:

private int mPreviousFirst;
...
listView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) { }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
      if(prevVisibleItem != firstVisibleItem){
        if (firstVisibleItem < mPreviousFirst) 
            getActionBar().show();
         else 
            getActionBar().hide();

        mPreviousFirst = firstVisibleItem;
      }
    }
});

我很接近,但出了点问题:(

3 个答案:

答案 0 :(得分:0)

您可以尝试在视图中操作以下onScrollChanged

onScrollChanged

跟踪滚动的当前和先前状态。如果你上升,那么你可以做同样的行为。

类似以下的内容可以实现您的要求:

@Override
protected void onScrollChanged (int currentHorizontalPosition, int currentVerticalPosition, int oldHorizontalPosition, int oldVerticalPosition)
{
   if(currentVerticalPosition < oldVerticalPosition && positionDifference > threshold)
   {
       getActionBar().show();
   }
   else //condition for downscroll or other actions.
   {
       getActionBar().hide();
   }
}

答案 1 :(得分:0)

您可以通过在每次更改时保存上一个第一个可见位置来执行此操作:

private int mPreviousFirst;
...
listView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) { }

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

        if (firstVisibleItem < mPreviousFirst) {
            getActionBar().show();
        } else if (firstVisibleItem > mPreviousFirst) {
            getActionBar().hide();
        }
        mPreviousFirst = firstVisibleItem;

    }
});

答案 2 :(得分:0)

   mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
        int mLastFirstVisibleItem = 0;

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

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (view.getId() == mListView.getId()) {
                final int currentFirstVisibleItem = mListView.getFirstVisiblePosition();

                if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                    // getSherlockActivity().getSupportActionBar().hide();
                    activity.hideActionBar();
                } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                    // getSherlockActivity().getSupportActionBar().show();
                    activity.showActionBar();
                }

                mLastFirstVisibleItem = currentFirstVisibleItem;
            }
        }
    });

这适用于kitkat