不能在RecyclerView中使用getLayoutPosition()或getAdapterPosition()

Tom*_*zyk 4 java android android-recyclerview

我真的不知道为什么我不能在课堂上使用getLayoutPosition();或使用getAdapterPosition();方法.

    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
        }
    }
Run Code Online (Sandbox Code Playgroud)

我可以使用getPosition()但不推荐使用以上方法.文件

Bla*_*elt 6

getLayoutPosition()并且getAdapterPosition()是支持库的第22版的一部分.您无法解决此问题,因为您仍在使用rev 21.更改

'com.android.support:recyclerview-v7:21.0.+'
Run Code Online (Sandbox Code Playgroud)

'com.android.support:recyclerview-v7:22.0.+'
Run Code Online (Sandbox Code Playgroud)

同步gradle,然后再试一次

  • 所以情况就是这样......我会记住这一课. (2认同)