到达网格视图结束时无限调用“onScroll”

时间:2017-10-20 12:19:28

标签: java android android-gridview

当我到达gridview的末尾时,我发现以下代码可以执行某些操作:

    GridView gridview = (YMAnimatedGridview) v.findViewById(R.id.my_gridview);
    gridview.setAdapter(adapter);
    final View footerView = mainView
            .findViewById(R.id.my_grid_footer_view);
    gridview.setOnScrollListener(new GridView.OnScrollListener() {

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {

            if (firstVisibleItem + visibleItemCount == totalItemCount) {

                // last item in grid is on the screen, show footer:
                fetchMoreItems();

            } 
        }

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

问题是,当我到达gridView的末尾时,会不断调用此方法。

private void fetchMoreItems() {
    Log.i(LOG_TAG, "Reached the end of grid view"); //for debbug purposes
    Integer lastIndex = thumbnails.length; //This is a Bitmap array with the data I'm alredy showing
    Cursor cursor = getData();
    thumbnails = concat(thumbnails, new Bitmap[30]);
    Integer lastIndexToRun = Math.min(30, cursor.getCount() - lastIndex);
    for(int j = 0; j < lastIndexToRun; j++) {
        Integer i = j + lastIndex;
        cursor.moveToPosition(i);
        thumbnails[i] = getThumbnail(i);
    }
    adapter.notifyDataSetChanged(); //This is the adapter used to fill the gridview
    cursor.close();
}

我认为我的错误应该在fetchMoreItems()方法中,但由于这是我第一次使用GridView,我不确定

2 个答案:

答案 0 :(得分:0)

您需要避免多次调用该方法,如下所示:

private int beforeLastItem;

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    final int lastItem = firstVisibleItem + visibleItemCount;
    if (lastItem == totalItemCount) {
        // last item in grid is on the screen, show footer:
        if (beforeLastItem != lastItem){ //to avoid multiple calls for the last item
            fetchMoreItems();
            beforeLastItem = lastItem;
        }
    } else {
        beforeLastItem = lastItem;
    }
}

答案 1 :(得分:0)

您可以使用布尔值来阻止多次调用:

    private final boolean isFetching = false;
    GridView gridview = (YMAnimatedGridview) v.findViewById(R.id.my_gridview);
    gridview.setAdapter(adapter);
    final View footerView = mainView
            .findViewById(R.id.my_grid_footer_view);
    gridview.setOnScrollListener(new GridView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {

            if (firstVisibleItem + visibleItemCount == totalItemCount && !isFetching) {
                isFetching = true;
                // last item in grid is on the screen, show footer:
                fetchMoreItems();

            }else{
                if(isFetching){
                    isFetching = false;
                }
            }
        }

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

您也可以在获取完成后将isFetching设置为false。