Get the position within the adapter's dataset for the view, where view is an adapter item or a descendant of an adapter item. - Android User Interface

Android examples for User Interface:AdapterView

Description

Get the position within the adapter's dataset for the view, where view is an adapter item or a descendant of an adapter item.

Demo Code


//package com.java2s;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Main {

    public static int getPositionForView(final AdapterView<?> adapterView,
            final View view) {
        int position = adapterView.getPositionForView(view);

        if (adapterView instanceof ListView) {
            position -= ((ListView) adapterView).getHeaderViewsCount();
        }/*from   w  w  w .j  av  a 2  s .com*/

        return position;
    }
}

Related Tutorials