Reputation: 97
I am creating multiple ImageViews dynamiclly in a loop. Every ImageView has a different position and a different image.
Finally i add them to a FrameLayout.
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(container.getWidth() / 3, container.getHeight() / 3);
ImageView imageView;
for(int i=0; i<cloths.size(); i++) {
imageView = new ImageView(getActivity(), null);
imageView.setAdjustViewBounds(true);
Glide.with(getActivity()).load(cloths.get(i).getImage()).into(imageView);
imageView.setOnTouchListener(touchListener);
params.leftMargin = (int) cloths.get(i).getxPos(container.getWidth());
params.topMargin = (int) cloths.get(i).getyPos(container.getHeight());
params.rightMargin = 0;
params.bottomMargin = 0;
imageView.setScaleY(cloths.get(i).getScale());
imageView.setScaleX(cloths.get(i).getScale());
container.addView(imageView, params);
}
Instead of positioning all imageviews correctly, they are all laying on top of each other at on the position of the last imageview.
Any ideas how to fix it ? What am i doing wrong ?
Upvotes: 0
Views: 181
Reputation: 5480
Assuming you know the desired position and size (getyPos()
suggest you do), you can try aggregating all the sizes so the images will go one over the other. If you don't know the sizes, you can use Glide to help you find them (or just measure the views after the image arrives).
public void putImages(FrameLayout container){
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(container.getWidth() / 3, container.getHeight() / 3);
int topMargin = 0;
for(int i=0; i<cloths.size(); i++) {
ImageView imageView = new ImageView(getActivity(), null);
imageView.setAdjustViewBounds(true);
Glide.with(getActivity()).load(cloths.get(i).getImage()).into(imageView);
imageView.setOnTouchListener(touchListener);
params.leftMargin = (int) cloths.get(i).getxPos(container.getWidth());
params.topMargin = topMargin;
params.rightMargin = 0;
params.bottomMargin = 0;
topMargin += (int) cloths.get(i).getyPos(container.getHeight());
imageView.setScaleY(cloths.get(i).getScale());
imageView.setScaleX(cloths.get(i).getScale());
container.addView(imageView, params);
}
}
Upvotes: 0