滚动时隐藏标题栏并在滚动后显示标题栏?

时间:2014-10-20 06:48:59

标签: android-listview scroll android-scrollview titlebar android-titlebar

我想在ListView中滚动项目时隐藏标题栏,我想在滚动后显示标题栏。建议任何想法来解决这个问题。

3 个答案:

答案 0 :(得分:0)

    //declare this two globally
    public static int ch = 0, cht = 1;
    int myLastVisiblePos;

    //Then add onScrollListener to your ListView

    list.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int currentFirstVisPos = view.getFirstVisiblePosition();
            if (currentFirstVisPos > myLastVisiblePos) {
                if (ch == 1) {
                    ch++;
                    cht = 1;
                    getActionBar().hide();
                } else if (ch == 0) {
                    getActionBar().show();
                    ch++;
                }
            }
            if (currentFirstVisPos < myLastVisiblePos)
                if (cht == 1)
                    getActionBar().show();
            myLastVisiblePos = currentFirstVisPos;
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
        }
    });

答案 1 :(得分:0)

首先将Xml View添加到ActionBar中,如下所示:

LayoutInflater inflater = (LayoutInflater) getActionBar()
            .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    View customActionBarView = inflater.inflate(R.layout.main, null);
final ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(
            ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);    

    actionBar.setCustomView(customActionBarView,
                    new ActionBar.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
            setContentView(R.layout.main);

然后在onScrollStateChanged()方法中进行更改:

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

    switch (scrollState) {
    case SCROLL_STATE_IDLE:
            actionBar.show();
            break;
    case SCROLL_STATE_TOUCH_SCROLL:
            actionBar.hide();
            break;
        }
    }

答案 2 :(得分:0)

萨拉姆。

这个解决方案非常适合我:

// mLastFirstVisibleItem defined globally

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

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        /**
         * Hide actionbar when scroll down
         */
        if (mLastFirstVisibleItem < firstVisibleItem)
            if (getSupportActionBar().isShowing())
                getSupportActionBar().hide();

        if (mLastFirstVisibleItem > firstVisibleItem)
            if (!getSupportActionBar().isShowing())
                getSupportActionBar().show();
        mLastFirstVisibleItem = firstVisibleItem;
    }
});

来源:Android ActionBar hide/show when scrolling list view