How to inflate a view in a determined position?
Asked Answered
R

1

7

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)

Rocketeer answered 30/12, 2010 at 6:22 Comment(0)
D
11

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:

Damaris answered 30/12, 2010 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.