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.
Related
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.
My layout is a bit complex.
I have a SwipeRefreshLayout in which I host a ListView. Whenever the user drags the Listview's top, the SwipeRefreshLayout performs a refresh. I also listen for the last visible item of the ListView to load next page of records (Endless scroll)
In the list's adaptor I have 2 views that I am using. The first one will only be visible in first row, the other view will remain the same for all other rows.
What I want to achieve:
On top of the row with position = 1 I want to have a sticky header. This means that when I scroll Up, the header will scroll to the top of the screen and will remain in there.
This sticky header will only be at one row
if possible I'd like to use a simple implementation as my layouts and adapters are already complex enough.
Waiting for your suggestions.
I didnt quite get your question the first time, heres the answer attempt round 2.
In your layout add an empty viewgroup (whichever you prefer, though linearlayout seems to work just great), add a scrollListener to your listView and check the position of your sticky view. If its top anchor is below (meaning its visible in the listview) the top of the screen you set the viewgroup visibility to gone, if the top anchor is either touching the top of the screen or below it, you add that view or one just like it to the viewgroup and set its visibility to visible.
You can adjust the position 2 view visibility accordingly to allow for this change to appear seamless. Can help you a bit more once you have some code and are on your way with this change.
I have a ListView that lives on top of another view.
The very first cell of the ListView is transparent so that you can see the view behind it and then scroll the rest of the contents over it.
I would like the background view to capture touch events only when the transparent cell is on top of it. Is this possible? I tried a million different approaches with overriding dispatch touch event in the first cell or on the listview but haven't had any success.
When you intercept dispatchTouchEvent, check your ListView, find the top cell (the transparent one), and if the x,y coordinates of the MotionEvent are within that cell, return false. Otherwise return true and your ListView will get the events.
you can also set a tag for the transparent cell and you can get it in onclick() item. you can do anything once you have determine the cell.
I want an expandable list in which I can drag a child item and drop it over some other parent list item which will result in child moving from one parent to other. I only need a direction how we can achieve this in general listview (let alone exapandable). Examples are available for picking up a listitem to change the order of listview i.e. to sort that list. How can I accomplish dropping over a view in order to group them.
Your best bet will be putting your effort on customizing and making your own listview, Consider extending AdapterView, this will give you more power over controlling child display and organizations, Putting animation such and drag and drop will be easier for you in this case, Otherwise with simple list view it might work but i doubt if it would generate the required result you want.
Here is a link for exending AdapterView and customizations, go through it it will give you enough confidance to put in your animation. I tried almost similar stuff and was succesfull by same way, unfortunately I dont have implemented code with me.
http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/
Else with list view try doing following,
Your listview should be wrapped inside a framelayout, you will need layers
Enable drawing cache for childrens, coz animation you seek requires playing with bitmaps
Second, when you touch a child in listview, get the bitmap of child, and inflate it at same coordinates of touched child, you can get position of Child easily.
Now time for some animation, you enable drag and drop over inflated bitmap, now when you move it, first thing you need to do is, shifting all the childrens in list view either Up or Down depending upon movement of finger, you can define somekind of threshold like unless half the height of children is moved you wont shift childs in listview up or down.
Moving child will be easy, all you need to do is applying Transformation animation to all the currently visible child in listview, use childCount and ChildAt api for the same, and animation set for playing them together.
Thats it when you build space by shifting child, user will feel like drag drop and shift, all the thing you need, when user places it a place, just modify your dataset underneath listview reposition it based on recent changes by user and refresh it,so that listview reorders itself.
I want to implement drag-and-drop in an android application to switch a child view from one custom view to another custom view (of the same type).
My problem is that the OnTouchEvent stops firing when leaving the direct parent while draging (in my case the custom view is build like this: RelativeLayout -> (TextView, Button, LinearLayout -> (*LinearLayouts containing *ImageViews)). I want to show a list of images wrapped in more rows if the images doesn't fit in one row...)
In fact i want to drag one of those imageviews (parent is a linearlayout-row, where the parent is a linearlayout where parent is a relativelayout) to another custom view of the same type. (it just has to be droped over the other view and be added to the other list...) but it always stops receiving the events when leaving its parent linear-layout.
Can you help me understand how the OnTouchEvent is handled when nested in different views? (already tried to add the OnTouchListener to every view and even the rootview of the activity)
To continue to receive touch events outside of your View, call getParent().requestDisallowInterceptTouchEvent(true) from onTouchEvent().