johannilsson/android-pulltorefresh

Can't get setSelection to work

Opened this issue ยท 11 comments

When I try to scroll to a specific position in the list and use setSelection() nothing happens.. no error, just nothing... smoothScrollBy works fine though.

Thank you for the report. This is strange, it's using setSelection internally to hide the header. Will investigate.

Hm worked for me. Could you try it in the test app ?

I did notice that calling setSelection within onCreate had no effect even though this works fine using the standardl ListView. But this works fine in the example app;

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    setSelection(5);
}

If i remove the method onAttachedToWindow in PullToRefreshListView the call from onCreate works as expected. I can't remember why I needed to override onAttachedToWindow now though. But please test by removing that and if nothing else breaks or anyone else have any problem with removing that method I'm happy to do it.

Great! Using it in the onCreate method was also where I ran into problems. I removed the onAttachToWindow method and now everything works fine. I could see no downsides or other affects from removing the method other then the list starting out with the 'touch to refresh' message being displayed by default. A setSelection(1) in my onCreate solved this.

Many thanks for your quick response!!

Ah that's why we needed it. Well then I don't want to remove it until that problem is solved. If you want to play around with it you can experiment by calling setSelection(1) from other places internally in the widget.

Hi guys.
I was having the same problem, and I also managed to fix it with your solution!
I simply had to remove the setSelection() call in the onAttachedToWindow() (or actually remove the onAttachedToWindow overriding).

Thanks guys.

Out of curiosity, do you remember why you called setSelection() in that method in the first place?

I ran into the same problem, and after investigation I found out the issue, as explained below by Romain Guy https://groups.google.com/forum/?fromgroups=#!topic/android-developers/EnyldBQDUwE

I created a new method :

private void resetPosition() {
  this.post(new Runnable() {
    @Override
    public void run() {
      setSelection(1);
    }
  }
}

and replaced all references of setSelection(1) to resetPosition()

and it worked!

I have the same problem, anyone here can help me?

I am having the same problem in API 28 Pie. setSelection doesn't work on Pie and scrollTo works first time only but after going to next fragment and coming back to same fragment where I am using ListView, scrollTo doesn't work. So strange behavior on Pie.

Hi, this library is deprecated and there is no active development in it, please have a look at SwipeRefreshLayout for an alternative.

Oh Sorry wrong window