Question

I want to hide actionbar on up-scrolling of listview and displaying actionbar again on down-scrolling of listview. Is there any method to detect up and down scrolling of listview? if yes, then please provide some reference.

Any help or guidance will be well appreciated.

Was it helpful?

Solution 3

API provides such methods, but they are protected, so the best practice in my opinion, is to create wrapper class (extend ScrollView) and make those methods public, particularly onScrollChanged and onOverScroll.

You may look at the following questions to see how to do this:

http://pastebin.com/ePeyswyQ

Detecting the scrolling direction in the adapter (up/down)

OTHER TIPS

Perhaps you can try the following to get this done:

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

This is from Google IO 2014's Android app. Provides a nice scroll up or down detection to show/hide the Toolbar after some amount of rows are scrolled.

private int mActionBarAutoHideSensivity = 0;
private int mActionBarAutoHideMinY = 0;
private int mActionBarAutoHideSignal = 0;
private boolean mActionBarShown = true;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    // ...

    ListView listView = (ListView) contentView.findViewById(R.id.listView);
    initActionBarAutoHide();
    listView.setOnScrollListener(new ListViewOnScrollListener());

    // ...
}

private void initActionBarAutoHide() {
    mActionBarAutoHideMinY = getResources().getDimensionPixelSize(R.dimen.action_bar_auto_hide_min_y); //152dp
    mActionBarAutoHideSensivity = getResources().getDimensionPixelSize(R.dimen.action_bar_auto_hide_sensivity); //48dp
}

public class ListViewOnScrollListener implements OnScrollListener {
    final static int ITEMS_THRESHOLD = 3;
    int lastFvi = 0;

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

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        onMainContentScrolled(firstVisibleItem <= ITEMS_THRESHOLD ? 0 : Integer.MAX_VALUE,
                lastFvi - firstVisibleItem > 0 ? Integer.MIN_VALUE :
                        lastFvi == firstVisibleItem ? 0 : Integer.MAX_VALUE
        );
        lastFvi = firstVisibleItem;
    }
}

private void onMainContentScrolled(int currentY, int deltaY) {
    if (deltaY > mActionBarAutoHideSensivity) {
        deltaY = mActionBarAutoHideSensivity;
    } else if (deltaY < -mActionBarAutoHideSensivity) {
        deltaY = -mActionBarAutoHideSensivity;
    }

    if (Math.signum(deltaY) * Math.signum(mActionBarAutoHideSignal) < 0) {
        // deltaY is a motion opposite to the accumulated signal, so reset signal
        mActionBarAutoHideSignal = deltaY;
    } else {
        // add to accumulated signal
        mActionBarAutoHideSignal += deltaY;
    }

    boolean shouldShow = currentY < mActionBarAutoHideMinY || (mActionBarAutoHideSignal <= -mActionBarAutoHideSensivity);
    autoShowOrHideActionBar(shouldShow);
}

protected void autoShowOrHideActionBar(boolean show) {
    if (show == mActionBarShown) {
        return;
    }

    mActionBarShown = show;
    if (show) {
        onScrollingUp();
    } else {
        onScrollingDown();
    }
}

public void onScrollingUp() {
    // ...
}

public void onScrollingDown() {
    // ...
}

Simple way to detect scroll up/down on android listview

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
  if(prevVisibleItem != firstVisibleItem){
    if(prevVisibleItem < firstVisibleItem)
      //ScrollDown
    else
      //ScrollUp

  prevVisibleItem = firstVisibleItem;
}

dont forget

yourListView.setOnScrollListener(yourScrollListener);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top