我有RecyclerView
其佈局爲GridLayout
3列。我正在使用ItemTouchHelper拖放RecyclerView
的GridLayout
。在我的適配器中有一個方法onItemMove()
,它通知我已移動的項目。它只會交換我已拖放的ArrayList
中的那些項目。但由於拖累和其他因素的變化,我想更新他們的位置以及ArrayList
。如何更新ArrayList中由於拖放回收站視圖的所有項目
這裏是我的Recyclerview的適配器:
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.MyViewHolder> implements com.sagar.quizdemo.helper.ItemTouchHelperAdapter {
String[] str = {"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9"};
List<String> itemList, actualList;
LayoutInflater inflater;
Context context;
private final OnStartDragListener mDragStartListener;
public GridAdapter(Context context, OnStartDragListener dragStartListener) {
this.context = context;
mDragStartListener = dragStartListener;
inflater = LayoutInflater.from(context);
itemList = new ArrayList<>();
actualList = new ArrayList<>(Arrays.asList(str));
}
public void getItemList(List<String> nameList) {
int currentSize = itemList.size();
itemList.clear();
itemList.addAll(nameList);
notifyItemRangeRemoved(0, currentSize);
notifyItemRangeInserted(0, nameList.size());
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_level_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.textView.setText(itemList.get(position));
// Start a drag whenever the handle view it touched
holder.cardView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
}
@Override
public int getItemCount() {
return itemList.size();
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Collections.swap(itemList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
notifyItemChanged(fromPosition);
notifyItemChanged(toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
itemList.remove(position);
notifyItemRemoved(position);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {
CardView cardView;
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.gameCard);
textView = (TextView) itemView.findViewById(R.id.gameText);
}
@Override
public void onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY);
}
@Override
public void onItemClear() {
itemView.setBackgroundColor(Color.WHITE);
}
}
}
如果最初的名單是:
{"Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9"}
後我換在RecyclerView第二和第四定位的元素。更新後的名單應該是這樣的(,我想):
{"Level 1", "Level 5", "Level 2", "Level 3", "Level 4", "Level 6", "Level 7", "Level 8", "Level 9"}
但我得到這個更新列表:
{"Level 1", "Level 5", "Level 3", "Level 4", "Level 2", "Level 6", "Level 7", "Level 8", "Level 9"}
我想檢查更新的列表。在哪裏可以在拖放後檢查適配器中的更新列表?我不能檢查'onItemMove()'裏面的列表。我想比較更新列表與另一個列表。 – XoXo
太棒了,它的工作。感謝您的幫助 – XoXo
@Hiren,你想檢查最後的組合是否正確嗎?那麼,當所有RecyclerView的動畫都完成後,我會建議這樣做。我認爲這可以通過使用自定義'ItemAnimator'建議在這個線程https://stackoverflow.com/questions/33710605/detect-animation-finish-in-androids-recyclerview – MeliX