I am trying to implement drag-and-drop functionality in my app using the DragLinearLayout class. I have successfully implemented the drag-and-drop feature. Now, I want to enable drag-and-drop when the user long-presses the layout and also add a delete button to each layout when drag-and-drop is enabled.
To achieve this, I read the views when the user long-presses the layout, place each layout inside a FrameLayout, add a close button to the FrameLayout, and then place the FrameLayout inside DragLinearLayout. However, I am facing an issue. I cannot drag the view when I add the FrameLayout. After intercepting the touch events for the FrameLayout, I can perform drag-and-drop, but I cannot click the close button. I have overridden the onTouch event for the close button as well, but it is not working. Please help me resolve this issue.
private void doLongPressProcess() {
for (int i = 0; i < dragLayout.getChildCount(); i++) {
View child = dragLayout.getChildAt(i);
child.setId(View.generateViewId());
child.setTag("" + i);
FrameLayout childFrame = getFrameLayout(i);
dragLayout.removeViewAt(i);
childFrame.addView(child);
dragLayout.addView(childFrame, i);
if (i != 0 && i != 2) {
//To enable drag and drop only for 0 and 2nd position
dragLayout.setViewDraggable(childFrame, childFrame);
ImageView closeButton = getImageView(child, dragLayout);
(childFrame).addView(closeButton);
} else {
dragLayout.disableViewDraggable(child, child);
}
}
}
@NonNull
private FrameLayout getFrameLayout(int i) {
FrameLayout childFrame = new FrameLayout(context) {
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
};
childFrame.setTag("Frame" + i);
childFrame.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT,
WRAP_CONTENT));
GradientDrawable border = new GradientDrawable();
border.setStroke(1, Color.BLACK); //black border with full opacity
childFrame.setBackground(border);
return childFrame;
}
@NonNull
private ImageView getImageView(View child, dragLayout dragLayout) {
ImageView closeButton = new androidx.appcompat.widget.AppCompatImageView(context) {
@Override
public void setOnTouchListener(OnTouchListener l) {
super.setOnTouchListener(l);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Notify parent that child might handle the click
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Handle the click event
performClick();
// Notify parent that child has handled the click
break;
case MotionEvent.ACTION_CANCEL:
// Cancel the click handling
break;
}
return super.onTouchEvent(event);
}
};
closeButton.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
closeButton.setImageResource(R.drawable.ic_close);
closeButton.setTag(child.getTag());
closeButton.setY(0);
closeButton.setX(0);
closeButton.setOnClickListener(v -> {
int size = dragLayout.getChildCount();
for (int i = 0; i < size; i++) {
if (dragLayout.getChildAt(i).getTag().equals(v.getTag())) {
dragLayout.removeViewInLayout(dragLayout.getChildAt(i));
break;
}
}
});
return closeButton;
}