Android自定义ViewGroup ,动态添加数目不确定的ImageView

开发中遇到这样的需求,在页面中显示数量不确定的图片。如果不用自定义ViewGroup的话,只能在layout中一个一个的添加ImageView。这样做显然是不合适的,而且会有相当多的重复代码。面对这样的需求,我们可以通过自定义ViewGroup来解决。

自定义的ViewGroup:

/**
 * Created by Answer on 2017/10/24.
 */

public class ImagesViewGroup extends ViewGroup {
    private int maringTop = 12;
    private List imageInfoList;
    private Context context;
    private ClickListener clickListener;

    public ImagesViewGroup(Context context) {
        this(context, null);
    }

    public ImagesViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImagesViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImagesViewGroup);
        maringTop = (int) a.getDimension(R.styleable.ImagesViewGroup_ivg_marginTop, 12);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        maringTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, maringTop, dm);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //控件总宽度
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = 0;
        int size = imageInfoList.size();
        for (int i = 0; i < size; i++) {
            height += (imageInfoList.get(i).height + maringTop);
        }
     
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (imageInfoList == null) {
            return;
        }
        int left = 0;
        int right = r;
        int top = t;
        int bottom = b;
        int size = imageInfoList.size();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                bottom = imageInfoList.get(i).height;
            } else {
                top = bottom + maringTop;
                bottom = imageInfoList.get(i).height + top;
            }
            ImageView imageView = new ImageView(context);
//            LayoutParams param = new MarginLayoutParams(imageInfoList.get(i).width, imageInfoList.get(i).height);
//            addView(imageView, param);
            addView(imageView);
            Glide.with(context).load(imageInfoList.get(i).imageUrl).fitCenter().into(imageView);
            imageView.layout(left, top, right, bottom);
            final int finalI = i;
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.imageClick(imageInfoList.get(finalI).width, imageInfoList.get(finalI).height,
                            imageInfoList.get(finalI).imageUrl, finalI);
                }
            });
    
        }
    }

    private void generateImageView(String imageUrl) {
        ImageView imageView = new ImageView(context);

    }

    public void setImageInfoList(List list) {
        this.imageInfoList = list;
    }

    public static class ImageInfo {
        public int width;
        public int height;
        public String imageUrl;

        public ImageInfo(int width, int height, String imageUrl) {
            this.width = width;
            this.height = height;
            this.imageUrl = imageUrl;
        }
    }

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface ClickListener {
        public void imageClick(int width, int height, String imageUrl, int pos);
    }
}

attrs.xml



    
        
    


layout.xml



    
    



MainActivity中设置数据并显示

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int screenWidth = this.getResources().getDisplayMetrics().widthPixels;
        ImagesViewGroup imagesViewGroup = (ImagesViewGroup) findViewById(R.id.imagesviewsgroup);
        List list = new ArrayList<>();
        list.add(new ImagesViewGroup.ImageInfo(screenWidth, (int) (screenWidth / ((float) 1200 / (float) 1600)), "xxx.jpg"));
        list.add(new ImagesViewGroup.ImageInfo(screenWidth, (int) (screenWidth / ((float) 2448 / (float) 3264)), "xxx.jpg"));
        list.add(new ImagesViewGroup.ImageInfo(screenWidth, (int) (screenWidth / ((float) 1200 / (float) 1600)), "xxx.jpg"));
        list.add(new ImagesViewGroup.ImageInfo(screenWidth, (int) (screenWidth / ((float) 1152 / (float) 1536)), "xxx.jpg"));

        imagesViewGroup.setImageInfoList(list);
        imagesViewGroup.setClickListener(new ImagesViewGroup.ClickListener() {
            @Override
            public void imageClick(int width, int height, String imageUrl, int pos) {
                System.out.println("pos   " + pos);
            }
        });

    }
}

你可能感兴趣的:(android)