Cannot use getLayoutPosition () or getAdapterPosition () in RecyclerView

I really don't know why I cannot use methods getLayoutPosition();either getAdapterPosition();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 it is deprecated, and methods are recommended above. documentation

+4
source share
1 answer

getLayoutPosition()and getAdapterPosition()are part of version 22 of the support library. You cannot solve 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

+6
source

All Articles