简体   繁体   English

LinearLayout(水平)-交换按钮位置

[英]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. 我有一个水平LinearLayout ,其中有4个图像按钮,它们的大小均相同,布局参数也相同。 I would like the user to be able to swap the positions of buttons 2 and 3 over. 我希望用户能够交换按钮2和3的位置。 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). 您只需确定它们的索引(您可能知道它们,也可以遍历布局中的所有子项以按其ID查找按钮的索引)。

  2. You remove one button with removeViewAt(int) . 您可以使用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中再次确定第二个按钮的索引。

  3. You add the removed button before another button with addView(View, int) . 您可以使用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() . 调用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 :) 得知动画已结束时,我替换了布局中的每个按钮:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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