package com.mjsheng.myappstore.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.mjsheng.myappstore.adapter.GaringRecommedAdapter;
import com.mjsheng.myappstore.bean.AppDateInfo;
import com.mjsheng.myappstore.callback.GalleryItemClickListener;


public class GaringRecommedLayout extends LinearLayout {

    private GaringRecommedAdapter mAdapter;
    private View[] mViews;
    private GalleryItemClickListener mListener;


    public GaringRecommedLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAdapter(GaringRecommedAdapter adapter) {
        removeAllViews();
        this.mAdapter = adapter;
        mViews = new View[adapter.getCount()];
        for (int i = 0; i < adapter.getCount(); i++) {
            final AppDateInfo info = (AppDateInfo) mAdapter.getItem(i);
            View view = adapter.getView(i, null, null);
            final int position = i;
            if (view != null) {
                view.setPadding(20, 0, 20, 0);
                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mListener != null) {
                            mListener.onGalleryItemClick(view, position);
                        }
                    }
                });
                view.setTag(info);
                LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                this.addView(view, layoutParams);
                mViews[i] = view;
            }
        }
    }

    public void setImageGalleryItemClickListener(GalleryItemClickListener listener) {
        mListener = listener;
    }
}
