I am working on a class that extends ViewGroup to order View items for a GridView.
I can easily add a new View element inside:
ImageView view = new ImageView(context); view.setImageBitmap(BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher)); addView(view);
Or removing a View item is also easy
removeViewAt(remove_index)
Changing an item can be done using
addView(new_index, removeViewAt(old_index));
but I want to duplicate the View element when one element is dragged on top of another. I tried to duplicate an item
addView(getChildAt(index))
And it shows an exception error
The specified child already has a parent. You must first call removeView () on the parent parent
I also tried to save all view elements in the list, called the removeAllView () method, and added the views in the class again.
ArrayList<View> children = new ArrayList<View>(); for (int i = 0; i < getChildCount(); i++){ children.add(getChildAt(i)); } children.add(getChildAt(index));
This still shows an exception error, as indicated above:
Bloating a view may work, but I want to copy the same view without accessing an external resource.
So, I want the method to separate this view from the parent ViewGroup and make several copies (duplicates) inside the class.
Any help is appreciated.