1

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.

When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.

Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).

Do you have any idea on how I can do that?

grebulon
  • 7,697
  • 5
  • 42
  • 66
Crixus
  • 21
  • 1
  • 1
  • 4

3 Answers3

2

i think you will find you happyness here

Community
  • 1
  • 1
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • Unfortunatly, it uses a ScrollView, and after some researches on Google, I've found that there is an unsolved issue with using a ListView inside a ScrollView (the ListView gets cut off). Thank you anyway :) – Crixus May 24 '11 at 00:20
  • 1
    There is no need to add a ListView in a ScrollView ,you can simply use a ScrollView :) , and add your Views to it:) and then you can autoScroll the ScrollView to the added view when you click the Button add :) – Houcine May 24 '11 at 00:34
  • That sounds good. I'm gonna try it tonight(I'm at work now). Thanks for your help :) – Crixus May 24 '11 at 09:31
2

ListView has a method just for this: smoothScrollToPosition

Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method

pheelicks
  • 7,461
  • 2
  • 45
  • 50
  • I hadn't noticed this method. The problem is that it has been added since Android 2.2 and I'm developping on an Android 2.1 device :(. I should consider upgrading my phone to Android 2.2(or 2.3) soon ^^. Thank you anyway :) – Crixus May 24 '11 at 09:31
  • did you find an answer for pre2.2? – scottyab Oct 05 '11 at 16:06
0

Try this one.. it will solve your problem, i tried it and it works great.

listView.post(new Runnable(){
    public void run() {
    listView.setSelection(listView.getCount() - 1);
}});
grebulon
  • 7,697
  • 5
  • 42
  • 66
Steve
  • 1,022
  • 2
  • 9
  • 30