为什么数据排序和recylerview视图排序不同

时间:2017-11-09 15:04:05

标签: android android-recyclerview

我写了一个RecyclerView,可以拖动其中的项目。当我拖动项目随机移动。然后我看到数据,适配器中数据的顺序和RecyclerView中显示的数据顺序是不一样的?为什么,谁能帮帮我? 代码如下:

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int dragFlag, swipeFlag;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        swipeFlag = 0;
    } else {
        dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP;
        swipeFlag = ItemTouchHelper.END;
    }
    return makeMovementFlags(dragFlag, swipeFlag);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    int fromPosi = viewHolder.getAdapterPosition();
    int toPosi = target.getAdapterPosition();
    adapter.move(fromPosi, toPosi);
    EventBus.getDefault().post(new EventMessage(ModifyMainMenuActivity.class.getSimpleName()));
    return true;
}

****并且遵循适配器***

public class ShopDecorationAdapter extends 
  BaseRecyclerViewAdapter<ProductListResponse.ProductItem> {

private int imageHeight, imageWidth;

public ShopDecorationAdapter(Context context, int layout, OnItemClickListener onItemClickListener) {
    super(context, layout, onItemClickListener);
}

public void setImageSize(int imgH, int imgW) {
    this.imageHeight = imgH;
    this.imageWidth = imgW;
}

@Override
protected void bindData(BaseViewHolder holder, ProductListResponse.ProductItem data, int position) {
    holder.getTextView(R.id.tv_product_name).setText(data.name);
    ViewGroup.LayoutParams imgLayoutParam = holder.getImageView(R.id.product_icon).getLayoutParams();
    imgLayoutParam.height = imageHeight;
    imgLayoutParam.width = imageWidth;
    holder.getItemView().setLayoutParams(imgLayoutParam);
    holder.getImageView(R.id.product_icon).setLayoutParams(imgLayoutParam);
    if (data.isAddBtn) {
        holder.getTextView(R.id.tv_product_name).setGravity(Gravity.CENTER_HORIZONTAL);

// ImgLoadUtils.loadLocalResourceDrawable(mContext,R.drawable.icon_add_in_teaching_record, // holder.getImageView(R.id.product_icon));             holder.getImageView(R.id.product_icon).setImageResource(R.drawable.icon_add_in_teaching_record);             holder.getTextView(R.id.tv_product_price).setVisibility(View.GONE);         } else {             holder.getTextView(R.id.tv_product_price).setVisibility(View.VISIBLE);             holder.getTextView(R.id.tv_product_price).setText(mContext.getString(R.string.im_product_price,data.zhimaPrice));             ImgLoadUtils.loadIntoUseFitWith(mContext,R.drawable.artisan_icon_miss,EncodeUtils.urlString(data.coverPic)                     ,holder.getImageView(R.id.product_icon));             if(data.isSelected&amp;&amp;!data.isAddBtn){                 。holder.getItemView()setBackgroundResource(R.drawable.bg_cf34270_border1dp_solid_white);

        } else {
            holder.getItemView().setBackgroundResource(R.drawable.bg_white);
        }
    }

}
public void move(int origin, int target) {
        Collections.swap(datas, origin, target);
        if (origin < target) {
            for (int i = origin; i < target; i++) {
                Collections.swap(datas, i, i + 1);
            }
        }
        if (origin > target) {
            for (int i = origin; i > target; i--) {
                Collections.swap(datas, i, i - 1);
            }
        }
        notifyItemMoved(origin, target);
    }

}

2 个答案:

答案 0 :(得分:0)

删除不必要的交换

public void move(int origin, int target) {
    //Collections.swap(datas, origin, target);  !!!!!!!!
    if (origin < target) {
        for (int i = origin; i < target; i++) {
            Collections.swap(datas, i, i + 1);
        }
    }
    if (origin > target) {
        for (int i = origin; i > target; i--) {
            Collections.swap(datas, i, i - 1);
        }
    }
    notifyItemMoved(origin, target);
}

答案 1 :(得分:0)

转到文件 - >设置 - &gt;构建,执行,部署 - &gt; Gradle-&gt;取消选中离线工作选项。