OnScrollListener更新GridView [英] OnScrollListener updating GridView

查看:204
本文介绍了OnScrollListener更新GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图,并希望在列表结束后更新网格视图中的项目。为了实现这个,我决定使用OnScrollListener。但是OnScrollListener的行为很奇怪,因为它调用我的方法来多次更新网格视图。

  @Override 
public void onScroll(AbsListView view,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
// TODO自动生成的方法存根

if(firstVisibleItem + visibleItemCount> = totalItemCount ){
//已到达
//调用方法来更新

Log.i(CountShit,String.valueOf(firstVisibleItem)++
+ String.valueOf(visibleItemCount)+> =
+ String.valueOf(totalItemCount));
}
}
});

并且在我的日志中,如果conditioin被多次调用,onScrollListener会一直监听我想要的滚动

pre $ 09-29 14:06:10.338:I / CountShit(23640):12 + 8> = 20
09-29 14:06:10.353:I / CountShit(23640):12 + 8> = 20
09-29 14:06:10.369:I / CountShit(23640):12 + 8> ; = 20
09-29 14:06:10.385:I / CountShit(23640):12 + 8> = 20
09-29 14:06:10.400: 12 + 8> = 20
09-29 14:06:10.424:I / CountShit(23640):14 + 6> = 20
09-29 14:06:10.439:I / CountShit (23640):14 + 6> = 20
09-29 14:06:10.455:I / CountShit(23640):14 + 6> = 20
09-29 14:06:1040 :I / CountShit(23640):14 + 6> = 20
09-29 14:06:10.486:I / CountShit(23640):14 + 6> = 20

有没有办法让我第一次打到列表的末尾,我的upate方法被调用..然后如果用户滚动向上或向下或者不应该再次调用该方法! (直到我到达我更新的课程列表的末尾)。

解决方案

我能够通过添加一些变量来解决问题

  private int previousTotal = 0; 
private boolean loading = true;
private int visibleThreshold = 5;

并检查以下条件。

<$
// TODO自动生成的方法存根
if(loading())$ p $ {
// TODO自动生成的方法存根
if(loading ){
if(totalItemCount> previousTotal){
loading = false;
previousTotal = totalItemCount;
pageCount ++;


if(!loading
&&(totalItemCount - visibleItemCount)< =(firstVisibleItem + visibleThreshold)){
// End已经达到
Log.i(CountShit,String.valueOf(firstVisibleItem)++
+ String.valueOf(visibleItemCount)+> =
+ String.valueOf(totalItemCount ));
//在这里调用update方法
loading = true;
}
}
});


I have a grid view and want to update the items in grid view after the end of list is reached. To implement this i decided to use OnScrollListener. But the behavior of OnScrollListener is strange as it calls my method to update grid view many times..

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

                if (firstVisibleItem + visibleItemCount >= totalItemCount) {
                    // End has been reached
                    // Call method to update

                Log.i("CountShit", String.valueOf(firstVisibleItem) + " + "
                        + String.valueOf(visibleItemCount) + " >= "
                        + String.valueOf(totalItemCount));
                }
            }
        });

and in my log the if conditioin is called many times as the onScrollListener keeps on listening the scroll i guess ..

09-29 14:06:10.338: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.353: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.369: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.385: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.400: I/CountShit(23640): 12 + 8 >= 20
09-29 14:06:10.424: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.439: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.455: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.470: I/CountShit(23640): 14 + 6 >= 20
09-29 14:06:10.486: I/CountShit(23640): 14 + 6 >= 20

Is there a way such that if i hit the end of list first time, my upate method is called.. and then if the user scrolls up or down or whatever the method should not be called again! (until i reach the end of my updated list ofcourse).

解决方案

I was able to solve the problem by adding some variables

        private int previousTotal = 0;
        private boolean loading = true;
        private int visibleThreshold = 5;

and checking the following condition ..

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                    pageCount++;
                }
            }
            if (!loading
                    && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                // End has been reached
                Log.i("CountShit", String.valueOf(firstVisibleItem) + " + "
                        + String.valueOf(visibleItemCount) + " >= "
                        + String.valueOf(totalItemCount));
                // call update method here
                loading = true;
            }
        }
    });

这篇关于OnScrollListener更新GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆