Search code examples
android-listviewscrollandroid-scrollviewtitlebarandroid-titlebar

Hide the Title bar while scrolling and Show the TItle bar after Scrolling?


I want to hide the title bar when i scrolling the items in the ListView and i want to show the title bar after scrolling. Suggest any ideas to solve this issue.


Solution

  • First add the Xml View into ActionBar like this:

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

    Then do the changes in onScrollStateChanged() method:

        @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;
            }
        }