我有一个水平 LinearLayout
,其中有 4 个图像按钮,大小均相同,布局参数也相同。我希望用户能够交换按钮 2 和 3 的位置。我在想他们也许可以在按钮上滑动来交换它们,或者可能在它们之间有另一个小按钮,并按下他们按下的箭头图像。
我的问题是我不确定如何在布局中交换按钮位置!我假设我需要获取布局中每个元素的索引,然后添加和删除它们,然后导致重新计算布局,以便重新绘制。这可能吗?
另外,如果能显示一些按钮移动到位的动画就好了,但我怀疑这是不可能的。
有什么建议吗?
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?
发布评论
评论(2)
是的,您可以交换按钮:
您只需确定它们的索引(您可能知道它们,或者您可以迭代布局中的所有子项以通过其 id 查找按钮的索引)。
您可以使用
removeViewAt( 删除一个按钮int)
。您应该在此处使用较大的索引,而不是在步骤 3 中再次确定第二个按钮的索引。您可以使用
addView(View, int)
。请注意,您将用作此方法参数的索引是第二个按钮的索引。调用
invalidate()
。Yes, you can swap your buttons:
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).
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.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.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 :)