Way to scroll up and scroll down an xml object - android

See it first: https://i.stack.imgur.com/uSgWY.jpg
For now I have the scroll of the button and the scroll of the button clicking the a button with view.animate().translationY(float);
But this is an animation and it's not scrolled by the finger.
Any ideas?

So, there is two ways:
Add onTouchListener and watch ACTION_DOWN, ACTION_MOVE and ACTION_UP events. In DOWN you need to remember touch position, then in MOVE calculate current difference between start position and current. And in UP same with MOVE.
Place your view inside of HorizontalScrollView and system will handle scrolling by itself.

Related

Custom View: swipe inside of scroll view

I created a custom View with Canvas and made it interactivly with overwriteing onTouchEvent.
But my view is inside of a ScrollView. It losts the focus while i drag/swipe an item to the right side in my custom view and move my finger a little bit up or down. Then the ScrollView react and my view never get an ACTION_UP Event.
Is there a way to avoid that the scrollView-Parent scolls while i interact with my custom view?
You need ViewParent.requestDisallowInterceptTouchEvent
https://developer.android.com/reference/android/view/ViewParent#requestDisallowInterceptTouchEvent(boolean)
In your custom view, once it has detected the touch gesture is for it, call scrollview.requestDisallowInterceptTouchEvent(true). The scrollview will then ignore further events for the duration of the touch i.e. until it (the scrollview) receives an ACTION_UP or ACTION_CANCEL event.

Handling longpress event of views inside scrollview in android

I have a ScrollView, inside that I have LinearLayout with 4 children. My problem is that my ScrollView consumes events and because of that I am not able to long press on my child views. I tried setting setLongClickable(false) but it is of no use, if I intercept touch events I'm not able to scroll my ScrollView.
Normally Android uses a long press to begin a drag in cases like these since it helps disambiguate when the user intends to longPress an item vs. scroll the item's container. But if you have an unambiguous signal when the user begins longpress an item, try getParent().requestDisallowInterceptTouchEvent(true) from the view when you know the user is beginning a press. This will prevent the ScrollView from intercepting touch events until the end of the current gesture.(check this link https://developer.android.com/reference/android/view/ViewParent.html#requestDisallowInterceptTouchEvent%28boolean%29)

Transferring touch event back to parent

I have a ListView inside a ScrollView. I know I can intercept the touch events using the onInterceptTouchEvent method in the ScrollView. But once the child, i.e. ListView, starts consuming the touch event, onInterceptTouchEvent is not call. My problem is how can I transfer the touch event back to parent(i.e. ScrollView) once the ListView scroll has reached top. I want the scroll to be in continuation.
Suppose I'm scrolling the ListView and have reached the top of the ListView I want the scroll to continue so that I can scroll the parent, i.e. ScrollView. How can I achieve this?
You can check whether you are at the top of the listview, then return false from the touch event.

Drag and drop between views

I have a viewgroup with textviews that are added dynamically into the viewgroup. How would I add the ability to drag and drop a textview between other textviews.
Is there a way to detect what view you have dropped it over.
If it makes it easier the textviews could be buttons.
On API 11 and up, you can use the built in functionality:
https://developer.android.com/guide/topics/ui/drag-drop.html
On previous versions, you can also do it by hand:
In theory, you have to add an onTouchListener for your views, which on action down saves the touch position relative to the view position.
Then, on receiving a touch move event, you set the position of the view to reflect that.
On receiving up event, the user has dropped the view, you check the current coordinates and compare them to the other views, if it is before another view, you move that view up and set the position of the moved view to be in line with the others again.
You can limit the dragging to only x/only y by only changing those values.

process touch events across multiple scrollviews

I have two scrollviews side by side, I want the user to be able to drag list items back and forth from left to right scrollviews. However, I can't find a way to handle the touch events. I can't set a touch listener for each scrollview seperately as the drag gesture gets dropped when passing from one to another. I tried creating an absolute layout over the top of both, which works from the drag and drop perspective, but it stops me from being able to scroll the scrollviews. Is there a simple solution to this? can anyone help me out?
Generally, onTouchListener returns a boolean that indicates whether the touch has been handled. It's up to you to decide whether the touch was handled or not. When the user touches a View, Android will call it's touch listener. If the touch listener returns true, then it regards the touch as handled then moves on. If the touch listener returns false, then it will go up one to the parent view (in this case whatever your ScrollView is). Then the parent view's touch listener is called and must decide how to handle the touch. It will keep cascading up the parent views until a true is returned or until it reaches the end.
In your case, you may have to decide what the user has to do in order to drag & drop vs. scrolling. Perhaps the user must do a long press on an item before he/she can drag it or something.

Categories

Resources