0

I basically populate a ListView lv with an ArrayAdapter and I need to programatically lv.smoothScrollToPosition(i) only after it's rendered; otherwise, it will not work. The event I'm listening to is onWindowFocusChanged(boolean hasFocus) to set the adapter to the listview.

How can I catch the moment at which is optimal to scroll the lv?

Jorge
  • 1,730
  • 20
  • 27

3 Answers3

1

I think you can work around for your situation. Using Handler, after you call notifyDataSetChanged or set adapter to your listview, you can use:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        //Do something after 200ms
        lv.smoothScrollToPosition(i);
      }
    }, 200);
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38
  • I complemented the solution with this answer and http://stackoverflow.com/a/3035521/2422833 – Jorge Mar 28 '17 at 20:04
0

try judge position in getView method;

public @NonNull View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

Letty
  • 1
  • 1
0

This can be done using overriden function getView in your ArrayAdapter. You can check if position is equal to the no of items in your list.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ...
    ...
    ...
    if(position == yourList.size() - 1) {
       //do your work
       //it's better to make interface handle this call inside your Activity
    }
}

Hope it helps

Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
  • Here, the number of times it iterates only match the number of items rendered (which I don't know in advance), not the size of the list. – Jorge Mar 28 '17 at 18:09