I have a dynamic data fetching VolleyRequest which is called onCreate with LazyLoading attached to the listview. But listview onScroll event is also fired after I fetch few records using the Volley request.
I tried setOnTouchListener of the listview and set a boolean variable but it didn't help.
setOnTouchListener Code
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;
}
});
onCreate Code
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 function is a volley request
LazyLoader class
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);
}