get RecyclerView Layout Position - Android User Interface

Android examples for User Interface:RecyclerView

Description

get RecyclerView Layout Position

Demo Code


import android.support.v7.widget.RecyclerView;
import android.view.View;

public class Main{
    /*from w w  w . j  a v a  2 s  .c  o m*/
    public static int getLayoutPosition(RecyclerView recyclerView,
            RecyclerView.ViewHolder holder) {
        RecyclerView.Adapter outerAdapter = recyclerView.getAdapter();
        if (outerAdapter != null
                && outerAdapter instanceof MTFRecyclerViewAdapterWrapper) {

            int headerViewCounter = ((MTFRecyclerViewAdapterWrapper) outerAdapter)
                    .getHeaderViewsCount();
            if (headerViewCounter > 0) {
                return holder.getLayoutPosition() - headerViewCounter;
            }
        }

        return holder.getLayoutPosition();
    }
}

Related Tutorials