我已经实现了将项目从一个recyclerView
拖放到另一个recyclerView
的功能,可以很好地处理阴影。我正在使用相同的适配器,因为两个Views
具有相同类型的数据。现在,我想自动实现此功能,这意味着一个项目应该开始拖动自身并转移到另一个recyclerView
中。
我在适配器中尝试过
if(pos==2){
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(holder.frameLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.frameLayout.startDragAndDrop(data, shadowBuilder, holder.frameLayout, 0);
} else {
holder.frameLayout.startDrag(data, shadowBuilder, holder.frameLayout, 0);
}
}
我正在使用自定义的`dragListener',它具有类似
的逻辑 public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
isDropped = true;
int positionTarget = -1;
String listType = "";
View viewSource = (View) event.getLocalState();
int viewId = v.getId();
final int flItem = R.id.frame_layout_item;
final int tvEmptyListTop = R.id.top_empty_textView;
final int tvEmptyListBottom = R.id.bottom_empty_textView;
final int rvTop = R.id.top_recyclerView;
final int rvBottom = R.id.bottom_recyclerView;
switch (viewId) {
case flItem:
case tvEmptyListTop:
case tvEmptyListBottom:
case rvTop:
case rvBottom:
RecyclerView target;
switch (viewId) {
case tvEmptyListTop:
case rvTop:
target = (RecyclerView) v.getRootView().findViewById(rvTop);
listType = "top";
break;
case tvEmptyListBottom:
case rvBottom:
target = (RecyclerView) v.getRootView().findViewById(rvBottom);
listType = "bottom";
break;
default:
target = (RecyclerView) v.getParent();
positionTarget = (int) v.getTag();
}
if (viewSource != null) {
RecyclerView source = (RecyclerView) viewSource.getParent();
RouterDevicesRecyclerViewAdapter adapterSource = (RouterDevicesRecyclerViewAdapter) source.getAdapter();
int positionSource = (int) viewSource.getTag();
int sourceId = source.getId();
Device list = adapterSource.getList().get(positionSource);
List<Device> listSource = adapterSource.getList();
listSource.remove(positionSource);
adapterSource.updateList(listSource);
adapterSource.notifyDataSetChanged();
RouterDevicesRecyclerViewAdapter adapterTarget = (RouterDevicesRecyclerViewAdapter) target.getAdapter();
List<Device> customListTarget = adapterTarget.getList();
if (positionTarget >= 0) {
customListTarget.add(positionTarget, list);
} else {
customListTarget.add(list);
}
adapterTarget.updateList(customListTarget);
adapterTarget.notifyDataSetChanged();
//Notify to the View
if (source != target) {
listener.setListType(listType, list.getId());
}
if (sourceId == rvBottom && adapterSource.getItemCount() < 1) {
listener.setEmptyListBottom(true);
}
if (viewId == tvEmptyListBottom) {
listener.setEmptyListBottom(false);
}
if (sourceId == rvTop && adapterSource.getItemCount() < 1) {
listener.setEmptyListTop(true);
}
if (viewId == tvEmptyListTop) {
listener.setEmptyListTop(false);
}
}
break;
}
break;
}
if (!isDropped && event.getLocalState() != null) {
((View) event.getLocalState()).setVisibility(View.VISIBLE);
}
return true;
}
当我手动更改项目视图时,它工作正常,但是现在我想自动移动项目,而无需使用用户的触摸操作。
我试图提供默认位置和目标视图,但对我而言不起作用。希望你能得到我的问题。谢谢。