【问题标题】:Can't use getLayoutPosition() or getAdapterPosition() in RecyclerView不能在 RecyclerView 中使用 getLayoutPosition() 或 getAdapterPosition()
【发布时间】:2015-06-22 09:28:46
【问题描述】:

我真的不知道为什么我不能在我的类中使用 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
        }
    }

我可以使用getPosition(),但已弃用,建议使用上述方法。 documentation

【问题讨论】:

标签: java android android-recyclerview


【解决方案1】:

getLayoutPosition()getAdapterPosition() 是支持库第 22 版的一部分。您无法解决它,因为您仍在使用 rev 21。更改

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

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

同步 gradle,然后重试

【讨论】:

  • 原来如此……我会记住教训的。
猜你喜欢
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2018-01-10
  • 1970-01-01
相关资源
最近更新 更多