Set drag and drop listener to SpannableString

I am trying to set OnDragListener to specific words of a TextView using SpannableString . Im can add OnClick listener using ClickableSpan

by doing

 hashText.setSpan(new ClickableSpan() { 

So, I decided that I would try the same, but replace ClickableSpan with OnDragListener .

I can read different drag and drop events, but I was not able to allocate drag and drop events for certain words that I select, since Im can do with ClickableSpan .

 hashText.setSpan(new View.OnDragListener() { @Override public boolean onDrag(View targetView, DragEvent event) { int action = event.getAction(); TextView targetVariable = (TextView) targetView; String textGetter; textGetter = targetVariable.getText().toString(); // boolean actionDropOutside = (DragEvent.ACTION_DRAG_ENDED != DragEvent.ACTION_DROP); switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: Log.d(TAG, "DRAG STARTED"); break; case DragEvent.ACTION_DRAG_ENTERED: ////create method that checks if text is empty if so lights up //if (textGetter.equals("")) { targetView.setBackgroundColor(CARD_SLECTED); //lightupVariableCard(cardV); Log.d(TAG, "DRAG ENTERED EMPTY TEXT"); //} Log.d(TAG, "DRAG ENTERED" + targetVariable.getText().toString()); break; case DragEvent.ACTION_DRAG_EXITED: targetView.setBackgroundColor(CARD_UNSLECTED); Log.d(TAG, "DRAG EXITED"); break; case DragEvent.ACTION_DROP: // Dropped, reassign View to ViewGroup //if (textGetter.equals("")) { TextView draggedView = (TextView) event.getLocalState(); ViewGroup owner = (ViewGroup) draggedView.getParent(); targetVariable.setText(draggedView.getText()); owner.setVisibility(View.INVISIBLE); targetView.setBackgroundColor(CARD_UNSLECTED); //fabDisplayCounter++; //displayFab(); Log.d(TAG, "DRAG DROPPED"); //} Log.d(TAG, "DRAG NOT POSSSIBLE HERE"); break; case DragEvent.ACTION_DRAG_ENDED: // if (actionDropOutside == true){ // Log.d(TAG, "DRAG DROPPED OUTSIDE TRUE"); // } Log.d(TAG, "DRAG ENDED"); //default: } return true; } }, matcher.start(), matcher.end(), i); the_question.setText(hashText); the_question.setMovementMethod(LinkMovementMethod.getInstance()); //the_question.setOnDragListener(new MyDragListener(hashText.nextSpanTransition())); 
+6
source share
1 answer

I am not a great specialist in this field, I have never had to dig it out. But since you have no answers, I am trying to give you some tips / directions.

I think you need a custom MovementMethod .

This package manages Android MovementMethod platforms: https://github.com/android/platform_frameworks_base/tree/master/core/java/android/text/method

In particular, this interface: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/MovementMethod.java

As you can see, it has an onTouchEvent() method.

ClickableSpan has an onClick method, but it only works because LinkMovementMethod processes touch events, looks for ClickableSpan and throws onClick() on them.

See: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/LinkMovementMethod.java

which are distributed: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/ScrollingMovementMethod.java

which are distributed https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/BaseMovementMethod.java

but the latter is not connected with touches.

ScrollingMovementMethod use the Touch class to delegate the handling of touch events, you can see this class here: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/method/Touch.java

Looking at the code very quickly, I think you can extend LinkMovementMethod and add drag and drop detection there. Then create a new Span type for Drag and call the drag and drop methods inside it.

I know that I have not given you a solution, but I hope that I have pointed you in the right direction.

+2
source

All Articles