Home » Android » android – getAdapterPosition() not returning position of item in RecyclerView

android – getAdapterPosition() not returning position of item in RecyclerView

Posted by: admin June 15, 2020 Leave a comment


This is a sort of follow-up or complement to this post. I am trying to get the position of an item in my RecyclerView, but none of the methods I have tried have worked.

I called getAdapterPosition() in my PersonViewHolder constructor and assigned its value to the integer position, which is used in the UnitOneFragment class to do something accordingly (see onClick() method). But whatever that something is, it doesn’t happen and my guess is because the getAdapterPosition() method doesn’t work or isn’t used correctly.

My adapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {

Context mContext;

RVAdapter(Context context, List<Chapter> chapters) {
    mContext = context;
    this.chapters = chapters;

public static CardView cv;
public static int position;
public static String chapterNumberTitle;

public class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView chapterName;
    TextView chapterNumber;
    // ImageView chapterPhoto;

    public PersonViewHolder(View itemView) {

        cv = (CardView) itemView.findViewById(R.id.cv);
        chapterName = (TextView) itemView.findViewById(R.id.chapter_name);
        chapterNumber = (TextView) itemView.findViewById(R.id.chapter_number);
        // chapterPhoto = (ImageView) itemView.findViewById(R.id.person_photo);

        chapterNumberTitle = chapterNumber.getText().toString();
        position = getAdapterPosition();


    public void onClick(View v) {

        Intent intent = new Intent(mContext, ChapterActivity.class);

List<Chapter> chapters;

public void onAttachedToRecyclerView(RecyclerView recyclerView) {

public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
    PersonViewHolder pvh = new PersonViewHolder(v);
    return pvh;

public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
    // personViewHolder.chapterPhoto.setImageResource(persons.get(i).photoId);


public int getItemCount() {
    return chapters.size();

The other class:

public class UnitOneFragment extends android.support.v4.app.Fragment {

private List<Chapter> chapters;
private RecyclerView rv;
private RecyclerView.LayoutManager llm;

public UnitOneFragment() {
    // Required empty public constructor

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_unit_one, container, false);

    getActivity().setTitle("Unit One");

    rv = (RecyclerView) v.findViewById(R.id.rv);

    llm = new LinearLayoutManager(getActivity());


    return v;

private void initializeData() {
    chapters = new ArrayList<>();
    chapters.add(new Chapter("Human Prehistory to the Early Civilizations", "Chapter One"));
    chapters.add(new Chapter("Classical China", "Chapter Two"));
    chapters.add(new Chapter("Classical India", "Chapter Three"));
    chapters.add(new Chapter("Classical Greece and Rome", "Chapter Four"));
    chapters.add(new Chapter("Classical Period: Declines and Diversities", "Chapter Five"));

private void initializeAdapter() {
    RVAdapter adapter = new RVAdapter(getActivity(), chapters);

public static void chapterChecker() {

    if (RVAdapter.position == 0) {
        SummaryFragment.summaryText.setText("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.");

class Chapter {
String chapterName;
String chapterNumber;
// int photoId;

Chapter(String chaptername, String chapternumber) {
    this.chapterName = chaptername;
    this.chapterNumber = chapternumber;
    // this.photoId = photoId;

My code seems pretty self-explanatory but if there’s anything confusing, please let me know and I will add more information. Once again, the problem is that I can’t get the position of the item in the RecyclerView and then act on it.

How to&Answers:

Call getAdapterPosition() in your onClick() method because then at the time of the click you want to know the position of the item. If you check at the time of the creation it will (most of the times) be inaccurate.


Can I get the click event using the viewHolder.mView.setOnClickListener() and then the viewHolder.getAdapterPosition () to get the position. How can I do to get some data from the item I’m clicking, for example the name.

Example complet here: