get Position At ListView - Android User Interface

Android examples for User Interface:ListView

Description

get Position At ListView

Demo Code


//package com.java2s;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Main {

    public static int getPositionAt(@NonNull ListView listView, int x, int y) {
        int firstPosition = listView.getFirstVisiblePosition();
        int lastPosition = listView.getLastVisiblePosition();
        for (int p = firstPosition; p <= lastPosition; p++) {
            View child = listView.getChildAt(p - firstPosition);
            if (child == null)
                continue;
            Rect hitRect = new Rect();
            child.getHitRect(hitRect);/* ww w . j  a  v a  2s .c  om*/
            if (hitRect.contains(x, y))
                return p;
        }
        return AdapterView.INVALID_POSITION;
    }
}

Related Tutorials