Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Drag Listener on SpannableString

Im trying to set an OnDragListener to certain words of a TextView using SpannableString. Im able to add an OnClick Listener by using ClickableSpan

by doing

hashText.setSpan(new ClickableSpan() {

So I figured I would try the same but replace ClickableSpan with OnDragListener.

I'm able to read the different drag events, but I have not been able to isolate the drag events to the specific words I choose, as Im able to 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()));
like image 449
user1446988 Avatar asked Mar 07 '16 23:03

user1446988


People also ask

How do I set the local listener in Oracle Database?

If you are using the Single Instance Oracle database, then you are using the local listener. You can set the local listener parameter as follows. ALTER SYSTEM SET LOCAL_LISTENER= ["] listener_address ["] [,...];

What is the drag event in JSFiddle?

The drag event is fired every few hundred milliseconds as an element or text selection is being dragged by the user. Continue the drag & drop operation. See this code in a JSFiddle demo or interact with it below. See implementation notes. The compatibility table on this page is generated from structured data.

How many listeners are there in Oracle Database?

Oracle database has two listener as follows. You can connect to database via Listeners if you connect from remote client. If you are using the Single Instance Oracle database, then you are using the local listener.


1 Answers

I'm not a big expert in this field, never had to dig it out. But since you have no replies I try to give you some hint / direction.

I think what you need is a custom MovementMethod.

The android platform MovementMethod are managed here inside this package: https://github.com/android/platform_frameworks_base/tree/master/core/java/android/text/method

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

As you see it has a onTouchEvent() method.

ClickableSpan has an onClick method, but it only works because there's a LinkMovementMethod handling the touch events, looking for ClickableSpan and issuing the onClick() on them.

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

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

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

but this last is not really related with touches.

ScrollingMovementMethod use the class Touch to delegate the touch event handling, 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 the LinkMovementMethod and add the drag detection there. Then create a new Span type for the Drag and call the dragging methods inside it.

I know I did not gave you a solution but I hope I pointed you in the right direction.

like image 161
Daniele Segato Avatar answered Oct 13 '22 03:10

Daniele Segato