Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.widget.ListView;

public class Main {
    public static void ensureVisible(ListView listView, int pos) {
        if (listView == null || pos < 0 || pos >= listView.getCount())
            return;

        int first = listView.getFirstVisiblePosition();
        int last = listView.getLastVisiblePosition();

        if (first < 0 || last < 0) {
            listView.setSelection(pos);
            return;
        }

        // check if we are in range (+/-1)
        if (pos >= first + 1 && pos <= last - 1)
            return;

        // try to center
        int center = last - first;
        center = center / 2;
        center = pos - center;
        if (center < 0)
            center = 0;
        listView.setSelection(center);
    }
}