两个片段之间的拖放视图 [英] Drag and Drop view between two Fragments
问题描述
我正在尝试在两个片段之间实现拖放。
我已经将它们添加到我的活动中,像这样
I'm currently trying to implement Drag and Drop between two Fragments. I already added them both to my Activity like this
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.leftContainer, new MainActivityFragment());
ft.add(R.id.rightContainer, new CartFragment());
ft.commit();
我的MainActivityFragment中有几个CardViews。我想把它们拖到我的CartFragment中的ListView中。然后,在CardView被删除之后,我将添加一个新项目到此ListView。
I have several CardViews in my MainActivityFragment. I'd like to drag them over to a ListView in my CartFragment. Then I'd add a new item to this ListView after the CardView has been dropped.
我已经在MainActivityFragment中添加了此代码到目前为止
I've added this code in my MainActivityFragment so far
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View currentView = inflater.inflate(R.layout.fragment_main, container, false);
firstProduct = (LinearLayout) currentView.findViewById(R.id.firstProduct);
firstProduct.setOnTouchListener(new MyTouchListener());
return currentView;
}
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.drag_overlay);
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
如果我想拖动CardView和ListView I想要放在同一个视图中,我只是说
If the CardView I want to drag and the ListView I want to drop it on would be in the same View, I'd just just say
listView.setOnDragListener(new MyDragListener());
但由于我无法访问ListView,因为它在不同的片段,我不知道如何这样做。
But since I can't access the ListView because it's in a different Fragment I have no idea how to do this.
我已经看到这个线程两个片段之间的拖放
但是我仍然无法弄清楚。
but still I can't figure it out.
希望我描述得很好,谢谢提前!
Hope I described it well enough, thanks in advance!
推荐答案
onDrag方法必须在该片段中实现将接受DragEvent.Action_Drop的情况,然后将此方法设置为要接受放弃视图的视图(Fragment视图)
The onDrag method must be implemented inside that fragment that will be accepting the DragEvent.Action_Drop case, and then you set this method on to the view(Fragment view) that you want to accept the dropped view
这篇关于两个片段之间的拖放视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!