gpt4 book ai didi

android - 在 android listview 中使用 onScroll

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:03:56 24 4
gpt4 key购买 nike

friend ,我正在使用 listview 并在到达末尾时在滚动上加载内容。但是我遇到了错误,比如如果快速滚动,有人可以帮助我解决这个问题,那么同一个 url 被调用了不止一次。我经历了 SCROLL_STATE_IDLE 但不知道如何在我的代码中实现相同的功能。即使我不触摸屏幕,它也会运行。我现在想知道如何在空闲状态下停止代码。在下面附上我的代码

int start = 0;
int limit = 3;
loadingMore = false;
listView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}

}

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


if ((lastInScreen == totalItemCount) ) {

if(loadingMore == false){
start = lastInScreen - 1;
url = "http://www.dskjhf.com/web-servic/request_response.php?type=get_articles&start="
+ start + "&end=" + limit;
grabURL(url);
}
}}
});

protected void grabURL(String url) {
// TODO Auto-generated method stub
new BackgroundTask().execute(url);
loadingMore = true;
}

最佳答案

检查了这个它会对你有帮助..对我来说它运行良好

        listView.setOnScrollListener(new OnScrollListener() {
private int currentVisibleItemCount;
private int currentScrollState;
private int currentFirstVisibleItem;
private int totalItem;
private LinearLayout lBelow;


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
this.currentScrollState = scrollState;
this.isScrollCompleted();
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;


}

private void isScrollCompleted() {
if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
/** To do code here*/

Page = Page + 1;
apiVariables = TCGAPIVariable.getSingletonObject();
searchUrl = apiVariables.searchList(cityId, catId, area,
keyword, cuisine, type, cost, "" + Page, "20",uId);
Log.d("SEARCH_URL", searchUrl.trim());
int totalRecord = Integer.parseInt(itemData.get(0)
.getTotalRecord());
if (totalRecord > totalItem) {
if (TCGStaticMethods
.isInternetAvailable(SearchActivity.this))
new SearchAsynTask(SearchActivity.this, searchUrl,
true).execute();
} else {
LinearLayout llBelow = (LinearLayout) findViewById(R.id.pbSearchLististingBelow);
llBelow.setVisibility(View.GONE);
}
}
}
});

关于android - 在 android listview 中使用 onScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26293461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com