简体   繁体   中英

LinearLayout (horizontal) - Swapping button positions

I have a horizontal LinearLayout which has 4 image buttons in it, all the same size with the same layout params. I would like the user to be able to swap the positions of buttons 2 and 3 over. I was thinking they could perhaps swipe over the buttons to swap them, or maybe have another small button between them with an arrow image that they pressed.

My problem is I'm not sure how I would swap the button positions over in the layout! I assume I need to get hold of the index of each in the layout and then add and remove them and then cause the layout to be recalculated so it is redrawn. Is this possible?

Also it would be nice to show some animation of the buttons moving in to position but I suspect this is just not possible.

Any advice?

Yes, you can swap your buttons:

  1. You just determine their indexes (you may know them or you can iterate through all children in the layout to find the indexes of the buttons by their id's).

  2. You remove one button with removeViewAt(int) . You should use here the index which is greater in magnitude not to determine the index of second button once again in the step 3.

  3. You add the removed button before another button with addView(View, int) . Note that the index you will use as the argument of this method is the index of the second button.

  4. Call invalidate() .

Right I've got it working using a transformation animation. I move each button, one left and one right to the location of the other button and then listen for the end of the transformation animation. On being informed the animation has ended I replace each button in the layout :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM