I have TextView
and ImageView
in one LinearLayout
. I want to drag first LinearLayout
to second LinearLayout
in such a manner when I drop the first linear to second LinearLayout
, its TextView
and ImageView
should come in first LinearLayout
and vice versa. I have done the R&D where I have found to drag and drop layout from first to second but the second layout is not coming in the first layout.
The problem is the layouts are not getting an exchange. I am able to drag and drop from first to second but unable to do first to second.
Below is my fragment :
public class MoreDestinationFragment extends Fragment implements View.OnClickListener, View.OnTouchListener, View.OnDragListener {
private OnFragmentInteractionListener mListener;
private LinearLayout llNews;
public MoreDestinationFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_more_destination, container, false);
llNews = view.findViewById(R.id.ll_new);
llNews.setOnClickListener(this);
llNews.setOnTouchListener(this);
llNews.setOnDragListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ll_new:
Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_newsDestinationFragment);
break;
}
@Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d("xxxxxxx", "Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("xxxxxxx", "Drag event entered into " + layoutview.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d("xxxxxxx", "Drag event exited from " + layoutview.toString());
break;
case DragEvent.ACTION_DROP:
Log.d("xxxxxxx", "Dropped");
View view = (View) dragevent.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d("xxxxxxx", "Drag ended");
break;
default:
break;
}
return true;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Please help me out and please advice the solution. Thanks.