0

我想在向上滚动列表视图时隐藏操作栏,并在向下滚动列表视图时再次显示操作栏。有什么方法可以检测列表视图的上下滚动吗?如果是,请提供一些参考。

任何帮助或指导将不胜感激。

4

4 回答 4

4

也许您可以尝试以下方法来完成此操作:

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();
    }
});
于 2013-03-28T12:02:30.297 回答
2

这是来自 Google IO 2014 的 Android 应用程序。提供一个很好的向上或向下滚动检测以在滚动一些行后显示/隐藏工具栏。

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() {
    // ...
}
于 2014-11-11T11:02:51.780 回答
1

API 提供了这样的方法,但它们是受保护的,所以我认为最好的做法是创建包装类(扩展 ScrollView)并将这些方法公开,特别是onScrollChangedonOverScroll.

您可以查看以下问题以了解如何执行此操作:

http://pastebin.com/ePeyswyQ

检测适配器中的滚动方向(上/下)

于 2013-03-28T12:04:43.430 回答
1

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

不要忘记

yourListView.setOnScrollListener(yourScrollListener);
于 2014-02-06T03:13:16.920 回答