RecycleView.V的方法getAdapterPosition()总是返回-1

时间:2017-01-17 02:14:55

标签: android

使用RecycleView时,getAdapterPosition()始终返回-1。我不知道原因。

我的onCreateViewHolder代码如下:

public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
    final ViewHolder holder = new ViewHolder(view);
    // add OnClickListener
    holder.mFruitView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = holder.getAdapterPosition();
            Fruit fruit = mFruitList.get(position);
            Toast.makeText(v.getContext(),"You Click : " + fruit.getName(),Toast.LENGTH_SHORT).show();
        }
    });
    return new ViewHolder(view);
}

我的ViewHolder代码如下:

static class ViewHolder extends RecyclerView.ViewHolder{
    View mFruitView;
    ImageView fruitImage;
    TextView fruitName;
    public ViewHolder(View itemView) {
        super(itemView);
        mFruitView = itemView;
        fruitImage = (ImageView) itemView.findViewById(R.id.iv_fruit);
        fruitName = (TextView) itemView.findViewById(R.id.tv_fruit);
    }
}

发生错误时的日志如下:

java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1

任何帮助都将不胜感激

3 个答案:

答案 0 :(得分:0)

我的适配器代码:

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

private static final String TAG = "FruitAdapter";
private List<Fruit> mFruitList;
public FruitAdapter(List<Fruit> fruitList){
    mFruitList = fruitList;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
    final ViewHolder holder = new ViewHolder(view);
    // add OnClickListener
    holder.mFruitView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fruit fruit = mFruitList.get(position);
            Toast.makeText(v.getContext(),"You Click : " + fruit.getName(),Toast.LENGTH_SHORT).show();
        }
    });
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Fruit fruit = mFruitList.get(position);
    holder.fruitImage.setImageResource(fruit.getImageId());
    holder.fruitName.setText(fruit.getName());
}

@Override
public int getItemCount() {
    return mFruitList.size();
}

static class ViewHolder extends RecyclerView.ViewHolder{
    View mFruitView;
    ImageView fruitImage;
    TextView fruitName;
    public ViewHolder(View itemView) {
        super(itemView);
        mFruitView = itemView;
        fruitImage = (ImageView) itemView.findViewById(R.id.iv_fruit);
        fruitName = (TextView) itemView.findViewById(R.id.tv_fruit);
    }
}
}

答案 1 :(得分:0)

修改适配器:

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private static final String TAG = FruitAdapter.class.getSimpleName();
    private List<Fruit> mFruitList;

    public FruitAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
        holder.mFruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "You Click : " + fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mFruitList == null) return 0;
        else return mFruitList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View mFruitView;
        ImageView fruitImage;
        TextView fruitName;

        public ViewHolder(View itemView) {
            super(itemView);
            mFruitView = itemView;
            fruitImage = (ImageView) itemView.findViewById(R.id.iv_fruit);
            fruitName = (TextView) itemView.findViewById(R.id.tv_fruit);
        }
    }
}

答案 2 :(得分:0)

getAdapterPosition()进行布局计算时,方法-1总是返回recyclerview。 您正在ViewHolder中调用此方法。这意味着RecyclerView正在执行计算。 如果您需要在视图的点击动作内部定位,请在公共void onClick(final View v)方法中对其进行调用,例如:

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private static final String TAG = "FruitAdapter";

private List<Fruit> mFruitList;

public FruitAdapter(List<Fruit> fruitList) {
    mFruitList = fruitList;
}

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.fruitImage.setImageResource(fruit.getImageId());
    holder.fruitName.setText(fruit.getName());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            Fruit fruit = mFruitList.get(position);
            Toast.makeText(v.getContext(), "You Click : " + fruit.getName(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public int getItemCount() {
            return mFruitList.size();
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            View mFruitView;
            ImageView fruitImage;
            TextView fruitName;

            public ViewHolder(View itemView) {
                super(itemView);
                mFruitView = itemView;
                fruitImage = (ImageView) itemView.findViewById(R.id.iv_fruit);
                fruitName = (TextView) itemView.findViewById(R.id.tv_fruit);
            }
        }
    }