Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inflate a view in a determined position?

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)

like image 416
Tivie Avatar asked Dec 30 '10 06:12

Tivie


1 Answers

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:

  • [LayoutInflater.inflate][1]
  • [Viewgroup.addView with index][2]

    [1]: http://developer.android.com/reference/android/view/LayoutInflater.html#inflate(int, android.view.ViewGroup) [2]: http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)

like image 69
Sebastian Roth Avatar answered Sep 28 '22 08:09

Sebastian Roth