我有一个网格视图,想要在到达列表结尾后更新网格视图中的项目。为了实现这一点,我决定使用OnScrollListener。但OnScrollListener的行为很奇怪,因为它调用我的方法多次更新网格视图..
@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));
}
}
});
并且在我的日志中多次调用if conditioin,因为onScrollListener一直在监听滚动我猜...
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
有没有一种方法,如果我第一次点击列表的末尾,我的upate方法被调用..然后如果用户向上或向下滚动或者不应该再次调用任何方法! (直到我到达我更新的课程列表的末尾)。
答案 0 :(得分:4)
你正在寻求一个无尽的观点。您必须创建一个无限滚动侦听器并将其应用于GridView。请尝试下面的代码;
初始化GridView并应用适配器
final GridView g = (GridView) findViewById(R.id.myGrid);
g.setAdapter(new ImageAdapter(this));
EndlessScrollListener scrollListener=new EndlessScrollListener(g,new RefreshList() {
@Override
public void onRefresh(int pageNumber) {
System.out.println("On Refresh invoked..");
}
});
g.setOnScrollListener(scrollListener);
<强> EndlessScrollListener 强>
public class EndlessScrollListener implements OnScrollListener {
private GridView gridView;
private boolean isLoading;
private boolean hasMorePages;
private int pageNumber=0;
private RefreshList refreshList;
private boolean isRefreshing;
public EndlessScrollListener(GridView gridView,RefreshList refreshList) {
this.gridView = gridView;
this.isLoading = false;
this.hasMorePages = true;
this.refreshList=refreshList;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (gridView.getLastVisiblePosition() + 1 == totalItemCount && !isLoading) {
isLoading = true;
if (hasMorePages&&!isRefreshing) {
isRefreshing=true;
refreshList.onRefresh(pageNumber);
}
} else {
isLoading = false;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void noMorePages() {
this.hasMorePages = false;
}
public void notifyMorePages(){
isRefreshing=false;
pageNumber=pageNumber+1;
}
public interface RefreshList {
public void onRefresh(int pageNumber);
}
}
答案 1 :(得分:1)
totalDataCount - &gt;变量,用于存储api为您提供的数据总数
previousTotal - &gt;存储先前总计数的变量。默认设置为零。
加载 - &gt;布尔值默认设置为false。
在您的活动中声明所有这些变量,而不是在内部类(下面)中声明
此代码可在以下位置使用: -
public class EndlessScrollListenerUser implements OnScrollListener {
boolean loading = true;
public EndlessScrollListenerUser() {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && lastInScreen==totalItemCount && totalDataCount>takeUser && (previousTotal)<totalDataCount) {
// Your API Call Goes Here after reaching end
loading = true;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
}
答案 2 :(得分:0)
我能够通过添加一些变量来解决问题
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
并检查以下情况..
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;
}
}
});