While using this, if i scroll a small amount, why is onScroll() called for 20 or so times i.e 5-6 times for each firstVisibleItem. Why not its called once for each scroll action?
listView.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("hey","called"+firstVisibleItem);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}
});
If you look at implentation of ListView, you can see that mOnScrollListener.onScroll is called only once in ListView, actually in invokeOnItemScrollListener() method.
void invokeOnItemScrollListener() {
if (mFastScroll != null) {
mFastScroll.onScroll(mFirstPosition, getChildCount(), mItemCount);
}
if (mOnScrollListener != null) {
mOnScrollListener.onScroll(this, mFirstPosition, getChildCount(), mItemCount);
}
onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these.
}
So let's try find some usages of invokeOnItemScrollListener(). We got a lot, including usages in AbsListView. In AbsListView, invokeOnItemScrollListener() is called indirectly every time in onTouchMove method. So now, it should be clear why you receive onScroll callback so many times.
Related
I'm using action bar pull to refresh of Chris Banes, and when I use the scroll listener to detect the bottom, it doesn't work.
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
Log.e("testing", "testing");
}
#Override
public void onScroll(AbsListView absListView, int i, int i2, int i3) {
Log.e("testing", "testing");
}
None of the methods are triggered.
Is there a way to use it? Or pull to refresh retain the listener for its use?
Done!
What I do was to enter this line
getListView().setOnScrollListener(this);
in onActivityCreated method. When I use it in others method, it was called just once.
I have chrisbanes PullToRefresh lib working in my project. Now I need to intercept scroll events to add some logic, it seems that the library is prepared to set onScrollListener, and I tried to add one with
myList.setOnScrollListener(this);
and
myList.getRefreshableView().setOnScrollListener(this);
but the listener methods are never called. I guess this has something to do with PullToRefreshBase class overriding onTouchEvent and onInterceptTouchEvent, but can't figure out how can I fix the issue, or maybe I'm missing something simple.
Did anyone succeeded adding onScrollListeners to PullToRefresh List?
For the change you will considered to listen the scroll bar .
To use the Scroll state change Listener I hope it will use to get the solution for you...
listStudies.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ((MainActivity.listsize - MainActivity.currentlistitemposition) <= 3
&& listagain == 0) {
listagain = 1;
System.out.println(MainActivity.listsize);
System.out.println(MainActivity.currentlistitemposition);
currentPage++;
try {
performRequest(currentPage);
} catch (Exception e) {
// Toast.makeText(MainActivity.this,
// "Error occuered during the Request...",
// Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
Because Am also using chrisbanes PullToRefresh lib So i hope it will usefull for you..Best of Luck...
I've implemented ListView in my application and working on onScrollListener. The ListView is working fine and in the onScroll() method of the onScrollListener, i'm trying to show a progressbar when the user has scrolled to the bottom.
The problem is that the listener doesn't stop listening and there are multiple progressbar loading on the screen if the user is trying to scroll even more. I don't have any idea how to solve this.
I want the onScrollListener to stop listening when the progressbar shows.
Please help
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (list.getLastVisiblePosition() == list.getAdapter().getCount() -
&& list.getChildAt(list.getChildCount() - 1).getBottom() <= list.getHeight()) {
ProgressBar pb = new ProgressBar(getActivity());
pb.setLayoutParams(pbParams);
linearLayout.addView(pb);
}
}
});
Thanks in advance
Can't you just check to see if there is already a progress bar and not add another?
I use Listview with Remote Control (Q1 Himedia SettopBOx)
Now down and up keydown ،How do I know where I am in listview
for example: when arrived in last item in listview then list refreshed or going to first item?
Please Help Me....
thanks
Take a look at OnScrollListener of ListView. You can set a new callback using ListView.setOnScrollListener() and get first visible item position and number of visible entries by implementing onScroll function of the listener.
Use an OnScrollListener for the listview like so.
myListView.setOnScrollListener(new OnScrollListener()
{
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount)
{
// Your code goes here. Determine the position with the firstVisibleItem.
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING)
{
Log.d(TAG, "User is flinging through the view");
}
}
});
We have used Custom List View inside that one Text View and two Edit text present. I have put On Item CLick Listener On the Edit text Listener is set properly. What Is problem when fling action is happen then that listener is not setting. Is their any other Listener which can help me to control fling action.
may be you can't control the flying action, you can listen the flying action:
ListView.OnScrollListener can listen the scroll action:
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == OnScrollListener.SCROLL_STATE_FLING){ //flying
}
}
you can try this, hope it can help you -):