android listview onscroll,using onScroll in android listview

Friends I am using listview and loading contents on scroll when it reaches at the end. But I am facing error like the same url is called more than once if if scrolled fastly can anybody help me to resolve this. I have gone thro SCROLL_STATE_IDLE but don't know how to implement the same in my code. Also even when am not touching screen its running. I now want to know how to stop the code on Idle state.Attaching my code below

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;

}

Checked this one it will help you..for me it's working well

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);

}

}

}

});

Check in the onScrollStateChanged method.

Value of SCROLL_STATE_IDLE is 0

setOnScrollListener(new OnScrollListener(){

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

// TODO Auto-generated method stub

}

public void onScrollStateChanged(AbsListView view, int scrollState) {

// TODO Auto-generated method stub

if(scrollState == 0) Log.d(TAG", "scrolling is idele...");

}

});

来源:https://stackoverflow.com/questions/26293461/using-onscroll-in-android-listview

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值