检测GridView底部和向上

时间:2014-09-09 06:57:00

标签: android gridview

我必须在GridView底部看到下一个上一个选项,并且如果GridView不在底部则不可见。我使用下面的代码它运行良好但在特定情况下if和else条件调用所以next-previous layout开始闪烁。

代码:

@Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub

            if (mGridView.getLastVisiblePosition() == mGridView.getAdapter()
                    .getCount() - 1
                    && mGridView.getChildAt(mGridView.getChildCount() - 1)
                            .getBottom() <= mGridView.getHeight()) {
                // End has been reached
                Animation slideUpIn = AnimationUtils.loadAnimation(
                        getActivity(), R.anim.side_in);
                mFooterView.startAnimation(slideUpIn);
                mFooterView.setVisibility(View.VISIBLE);
            } else {
                mFooterView.setVisibility(View.GONE);
            }

    }

任何建议都将非常感谢。

1 个答案:

答案 0 :(得分:0)

您的if (mGridView.getLastVisiblePosition() == mGridView.getAdapter().getCount() - 1 && mGridView.getChildAt(mGridView.getChildCount() - 1).getBottom() <= mGridView.getHeight())正在创建滞后。

onScroll()

totalItemCountmGridView.getAdapter().getCount() firstVisibleItem+visibleItemCountmGridView.getLastVisiblePosition()

请参阅此文档:http://developer.android.com/reference/android/widget/AbsListView.OnScrollListener.html#onScroll(android.widget.AbsListView,int,int,int)

修改 if(firstVisibleItem == 0) means you are on the top.

列表的底部:

if(firstVisibleItem + visibleItemCount >= totalItemCount){
    // End has been reached
}

编辑:检测向上滚动

int myLastVisiblePos = 0; //global variable in the activity class

gridView.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
            int currentFirstVisPos = view.getFirstVisiblePosition();
            if(currentFirstVisPos > myLastVisiblePos) {
                //scroll down
            }
            if(currentFirstVisPos < myLastVisiblePos) {
                //scroll up
            }
            myLastVisiblePos = currentFirstVisPos;
        }
    }
});