滚动时隐藏Android工具栏

时间:2014-12-17 17:55:02

标签: android android-listview scroll android-toolbar

如果我在ListFragment中向下滚动ListFragment(实现AbsListView.OnScrollListener),我想要隐藏我的工具栏,如果我向上滚动则再次显示它。我使用这段代码,但它显示了一个闪烁的工具栏:

    listView.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() == listView.getId()) {
                final int currentFirstVisibleItem = listView.getFirstVisiblePosition();

                if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                    // getSherlockActivity().getSupportActionBar().hide();
                    ((ActionBarActivity)getActivity()).getSupportActionBar().hide();
                } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                    // getSherlockActivity().getSupportActionBar().show();
                    ((ActionBarActivity)getActivity()).getSupportActionBar().show();
                }

                mLastFirstVisibleItem = currentFirstVisibleItem;
            }
        }
    });

修改 我发现了一个问题,因为我已经使用OnScrollListener来检测是否到达了ListView的末尾。但我怎样才能避免冲突?

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    // Leave this empty
}

/**
 * Method to detect if scrolled to end of listview
 *
 * @param listView
 * @param scrollState
 */
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
    if (scrollState == SCROLL_STATE_IDLE) {
        int threshold = 0;
        if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - threshold) {
            Log.d(TAG, "Scrolled to end of list, load more articles");
            currentPage++;
            Log.d(TAG, "CurrentPage for EndlessAdapter:" + currentPage);
            // Load more articles
            loadMoreArticles(currentPage);
        }
    }
}

任何想法或改进建议?

0 个答案:

没有答案