我有一个动态数据获取VolleyRequest,它被称为onCreate,并将LazyLoading附加到列表视图。但是,在我使用Volley请求获取一些记录之后,也会触发listview onScroll事件。
我尝试了listview的setOnTouchListener并设置了一个布尔变量,但这没有帮助。
setOnTouchListener代码
lstViewPlayers.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(view == lstViewPlayers && motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
userScrolled=true;
}
return false;
}
});
创建代码
lstViewPlayers=findViewById(R.id.lstViewPlayers);
customAdapterViewUserTeamNew=new CustomAdapterViewUserTeamNew(ViewUsersTeamLive.this,arr_name,arr_playername);
lstViewPlayers.setAdapter(customAdapterViewUserTeamNew);
if(new ConnectionDetector(ViewUsersTeamLive.this).is_internet_connected()){
// Index is required to fetch the next set of items
int startIndex = customAdapterViewUserTeamNew.getCount();
getUserTeams(startIndex);
}
else{
Toast.makeText(ViewUsersTeamLive.this,"Please connect to working internet",Toast.LENGTH_SHORT).show();
}
lstViewPlayers.setOnScrollListener(new LazyLoader() {
@Override
public void loadMore(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// Fetch your data here !!!
if(new ConnectionDetector(ViewUsersTeamLive.this).is_internet_connected()) {
if(userScrolled==true) {
int startIndex = customAdapterViewUserTeamNew.getCount();
getUserTeams(startIndex);
}
}
else{
Toast.makeText(ViewUsersTeamLive.this,"Please Connect to Working Internet",Toast.LENGTH_SHORT).show();
}
}
});
getUserTeam 函数是截击请求
LazyLoader 类
public abstract class LazyLoader implements AbsListView.OnScrollListener {
private static final int DEFAULT_THRESHOLD = 15 ;
//public static boolean userScrolled=false;
private boolean loading = true ;
private int previousTotal = 0 ;
private int threshold = DEFAULT_THRESHOLD ;
public LazyLoader() {}
public LazyLoader(int threshold) {
this.threshold = threshold;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
/*if(scrollState==AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){
userScrolled=true;
}
else{
userScrolled=false;
}*/
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(loading) {
if(totalItemCount > previousTotal) {
// the loading has finished
loading = false ;
previousTotal = totalItemCount ;
}
}
// check if the List needs more data
if(!loading && ((firstVisibleItem + visibleItemCount ) >= (totalItemCount - threshold))) {
loading = true ;
// List needs more data. Go fetch !!
loadMore(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
}
// Called when the user is nearing the end of the ListView
// and the ListView is ready to add more items.
public abstract void loadMore(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount);
}