java.lang.IndexOutOfBoundsException:检测到不一致。无效的视图持有者适配器positionViewHolder {5280f810 position = 0 id = -1,oldPos = 0,

时间:2016-12-09 07:11:39

标签: android android-recyclerview

以下方法有问题,但我不知道如何修复它:

mDataSet.remove(position - 1);
notifyItemRemoved(position);
notifyItemRangeChanged(0, getItemCount());

全班:

public class DemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private Context mContext;
    private List<String> mDataSet;

    private MyViewHolder normalHolder;
    private HeaderHolder headerHolder;

    public DemoAdapter(List<String> mDataSet, Context context) {
        this.mContext = context;
        this.mDataSet = mDataSet;
    }

    public void setData(List<String> mDataset) {
        this.mDataSet = mDataset;
        notifyDataSetChanged();
    }

    public void Remove(int position) {
        mDataSet.remove(position);
        notifyItemRemoved(position);
    }

    public void Add(String text, int position) {
        mDataSet.add(position, text);
        notifyItemInserted(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        if (viewType == TYPE_HEADER) {
            //inflate header layout
            View headerview = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main_recyclerview_headerview, parent, false);
            headerHolder = new HeaderHolder(headerview, mContext);
            return headerHolder;

        } else if (viewType == TYPE_NORMAL) {
            View normalView = LayoutInflater.from(context).inflate(R.layout.activity_main_recyclerview_itemview, parent, false);
            normalHolder = new MyViewHolder(normalView, mContext);
            return normalHolder;
        }

        // set the view's size, margins, paddings and layout parameters
        //return viewHolder;
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

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

        LogUtils.I("COCO", " bind position:" +  position );

        if (holder instanceof HeaderHolder) {
            //test headerview's click listener happens correctly
            headerHolder = (HeaderHolder) holder;

            headerHolder.image.setOnClickListener(this);

        } else if (holder instanceof MyViewHolder) {
            //to avoid outofindexexception, as header size = 1
            String data = mDataSet.get(position - 1);
            normalHolder = (MyViewHolder) holder;

            normalHolder.mTextview.setOnClickListener(this);
            normalHolder.ll_itemView.setOnClickListener(this);
            normalHolder.ll_itemView.setOnLongClickListener(this);
            normalHolder.mTextview.setOnLongClickListener(this);

            normalHolder.mTextview.setTag(position);
            normalHolder.ll_itemView.setTag(position);

            normalHolder.mTextview.setText(data);

        }
    }

    @Override
    public int getItemCount() {
        if (null != mDataset && mDataset.size() > 0)
            //return mDataSet.size();
            //+1 is header's size
            return mDataSet.size() + 1;

        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_HEADER;

        return TYPE_NORMAL;
    }

    @Override
    public boolean onLongClick(View view) {

        switch (view.getId()) {
            case R.id.activity_main_recyclerview_itemview:
                //click to remove itemview, as existing headerview so position -1
                int position = (int) view.getTag();
                mDataSet.remove(position - 1);
                notifyItemRemoved(position);
                notifyItemRangeChanged(0, getItemCount());

                LogUtils.I("COCO", "data position:" +  view.getTag() );
                LogUtils.I("COCO", " item position:" +  position );
                break;

            case R.id.activity_main_recyclerview_itemview_textview:
                LogUtils.I("COCO", "long click content:" + normalHolder.mTextview.getText());
                break;
        }
        return true;
    }
}

0 个答案:

没有答案