如何在确定的位置膨胀视图?

发布于 2024-10-09 20:11:43 字数 773 浏览 2 评论 0原文

我有一个带有“添加”和“删除”按钮的活动。

“添加按钮”会膨胀视图,“删除按钮”会删除选定的视图。

当我使用“添加按钮”膨胀视图时,它会自动绘制在任何先前膨胀的视图下方。

我想让用户能够上下移动每个膨胀视图,这样他就可以更改它们在屏幕中显示的顺序(以拖放效果)

+---------------------+     +---------------------+
| +-----------------+ |     | +-----------------+ | 
| |      View 1     | |     | |      View 2     | |  
| +-----------------+ |     | +-----------------+ |
|                     | --> |                     |
| +-----------------+ |     | +-----------------+ |
| |      View 2     | |     | |      View 1     | |
| +-----------------+ |     | +-----------------+ |  
+---------------------+     +---------------------+

是否可以指定“位置”其中每个视图都膨胀了? (例如,在当前选定视图的下方或上方?)

如果不是,实现所需效果的最佳方法是什么?

(如果您认为有帮助,请随时询问任何代码)

I have an activity with an "Add" and a "Delete" button.

The "Add Button" inflates a view, the "Delete Button" removes a selected View.

When I inflate a view with the "Add Button", it is automatically drawn below any previously infalted view.

I would like to give the user the possibility to move each inflated view up and down, so he could change the order they are show in screen (in a drag'n'drop kind of effect)

+---------------------+     +---------------------+
| +-----------------+ |     | +-----------------+ | 
| |      View 1     | |     | |      View 2     | |  
| +-----------------+ |     | +-----------------+ |
|                     | --> |                     |
| +-----------------+ |     | +-----------------+ |
| |      View 2     | |     | |      View 1     | |
| +-----------------+ |     | +-----------------+ |  
+---------------------+     +---------------------+

Is it possible to specify the "position" in which each view is inflated? (for instance, below or above the currently selected view?)

If not, what is the best way to accomplish the desired effect?

(feel free to ask for any piece of code, if you think it might help)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2024-10-16 20:11:43

您的视图看起来像一个垂直的LinearLayout。所以我建议你使用类似的东西:

View v = inflater.inflate(R.layout.XXXX, null);
yourLinearLayout.addView(v, INDEX, new LinearLayout.LayoutParams(...)

使用索引(你需要以某种方式确定)你可以控制位置。 LinearLayout是ViewGroup的子类,因此该方法可用。

文档:

Your view looks like a vertical LinearLayout. So I would recommend you use something like:

View v = inflater.inflate(R.layout.XXXX, null);
yourLinearLayout.addView(v, INDEX, new LinearLayout.LayoutParams(...)

Using the INDEX (you need to determine somehow) you could control the position. LinearLayout is a child class of ViewGroup, so the method is available.

Docs:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文