5

I really don't know why I can't use getLayoutPosition(); or getAdapterPosition(); methods inside my class.

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView image;
        private TextView title;
        private TextView price;

        public MyViewHolder(View itemView) {
            super(itemView);
            image = (ImageView)itemView.findViewById(R.id.horizontal_list_image);
            title = (TextView)itemView.findViewById(R.id.horizontal_list_title);
            price = (TextView)itemView.findViewById(R.id.horizontal_list_price);
            image.setOnClickListener(this);
            title.setOnClickListener(this);
            price.setOnClickListener(this);
        }
        public ImageView getImage() {
            return image;
        }
        public TextView getTitle() {
            return title;
        }
        public TextView getPrice() {
            return price;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "CLICK", Toast.LENGTH_SHORT).show();
            getLayoutPosition(); //no method like that
        }
    }

I can use however getPosition() but is deprecated and above methods are advised. documentation

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
  • [http://stackoverflow.com/questions/30951551/how-to-get-other-views-in-same-viewholder-at-same-position-as-the-onclicked-view/30951600#30951600](http://stackoverflow.com/questions/30951551/how-to-get-other-views-in-same-viewholder-at-same-position-as-the-onclicked-view/30951600#30951600) – M D Jun 22 '15 at 09:30
  • which version of support:recyclerview are you using ? – Blackbelt Jun 22 '15 at 09:32
  • @Blackbelt `import android.support.v7.widget.RecyclerView;` – Tomasz Mularczyk Jun 22 '15 at 09:33
  • yes got it. Check which reversion are you using. It is in your module build.gradle – Blackbelt Jun 22 '15 at 09:34
  • @Blackbelt sorry, didn't understand what you mean. `'com.android.support:recyclerview-v7:21.0.+'` – Tomasz Mularczyk Jun 22 '15 at 09:37

1 Answers1

6

getLayoutPosition() and getAdapterPosition() are part of revision 22 of the support library. You are not able to resolve it because you are still using rev 21. Change

'com.android.support:recyclerview-v7:21.0.+'

to

'com.android.support:recyclerview-v7:22.0.+'

sync gradle, and try again

Blackbelt
  • 156,034
  • 29
  • 297
  • 305