Ashok
Ashok

Reputation: 839

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.

Upvotes: 0

Views: 3470

Answers (3)

Sayed Abolfazl Fatemi
Sayed Abolfazl Fatemi

Reputation: 3911

This solution worked for me very good:

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

Source: Android ActionBar hide/show when scrolling list view

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

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

Upvotes: 0

Ashok
Ashok

Reputation: 839

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

Upvotes: 0

Related Questions