Last active
November 8, 2016 17:26
-
-
Save tranquangchau/296d6c48a839bcbbe668b1345cb93228 to your computer and use it in GitHub Desktop.
listview check when scroll end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/a/32904001 | |
package com.javahelps.externaldatabasedemo; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.widget.AbsListView; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.List; | |
public class MainActivity extends ActionBarActivity { | |
private ListView listView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
this.listView = (ListView) findViewById(R.id.listView); | |
DatabaseAccess_VH databaseAccess = DatabaseAccess_VH.getInstance(this); | |
databaseAccess.open(); | |
List<String> quotes = databaseAccess.getQuotes(); | |
databaseAccess.close(); | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes); | |
this.listView.setAdapter(adapter); | |
listView.setOnScrollListener(new OnScrollFinishListener() { | |
@Override | |
protected void onScrollFinished() { | |
Toast.makeText(MainActivity.this, "Endshow", Toast.LENGTH_SHORT).show(); | |
// do whatever you need here! | |
} | |
}); | |
} | |
} | |
abstract class OnScrollFinishListener implements AbsListView.OnScrollListener { | |
int mCurrentScrollState; | |
int mCurrentVisibleItemCount; | |
boolean n_same=false; //variable check if end | |
@Override | |
public void onScrollStateChanged(AbsListView view, int scrollState) { | |
mCurrentScrollState = scrollState; | |
if (isScrollCompleted()) { | |
onScrollFinished(); | |
} | |
} | |
@Override | |
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { | |
mCurrentVisibleItemCount = visibleItemCount; | |
if((firstVisibleItem + visibleItemCount) == totalItemCount) | |
{ | |
n_same=true; | |
}else{ | |
n_same=false; | |
} | |
} | |
private boolean isScrollCompleted() { | |
return mCurrentVisibleItemCount > 0 && mCurrentScrollState == SCROLL_STATE_IDLE && n_same==true; | |
} | |
protected abstract void onScrollFinished(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test ok scroll class check end