SingleImageViewRecyclerViewAdapter.java 4.03 KB
package com.cnlive.goldenline.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.base.BaseRecyclerAdapter;
import com.cnlive.goldenline.ui.widget.coverflow.ViewUtil;
import com.cnlive.goldenline.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:单ImageView RecyclerView 适配器
 * Created on 2017/3/6
 * Author : 萧
 */
public class SingleImageViewRecyclerViewAdapter extends BaseRecyclerAdapter<String> {

    private int designWidth, designHeight;
    private int leftmargin, rightMargin, topMargin, bottomMargin;

    public SingleImageViewRecyclerViewAdapter(Context context) {
        super(context);
    }

    public SingleImageViewRecyclerViewAdapter(Context context, int designWidth, int designHeight) {
        super(context);
        this.designWidth = designWidth;
        this.designHeight = designHeight;
    }

    public int getDesignWidth() {
        return designWidth;
    }

    public void setDesignWidth(int designWidth) {
        this.designWidth = designWidth;
    }

    public int getDesignHeight() {
        return designHeight;
    }

    public void setDesignHeight(int designHeight) {
        this.designHeight = designHeight;
    }

    public int getLeftmargin() {
        return leftmargin;
    }

    public void setLeftmargin(int leftmargin) {
        this.leftmargin = leftmargin;
    }

    public int getRightMargin() {
        return rightMargin;
    }

    public void setRightMargin(int rightMargin) {
        this.rightMargin = rightMargin;
    }

    public int getTopMargin() {
        return topMargin;
    }

    public void setTopMargin(int topMargin) {
        this.topMargin = topMargin;
    }

    public int getBottomMargin() {
        return bottomMargin;
    }

    public void setBottomMargin(int bottomMargin) {
        this.bottomMargin = bottomMargin;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_single_img, parent, false);
        return new SingleImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SingleImageViewHolder) {
            SingleImageViewHolder viewHolder = (SingleImageViewHolder) holder;
            viewHolder.show(mContext, mItems.get(position));
        }
    }

    class SingleImageViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.single_img)
        ImageView img;

        public SingleImageViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void show(Context mContext, String s) {
            if (mContext != null && !StringUtils.isEmpty(s)) {
                if (designHeight > 0 && designWidth > 0) {
                    ViewGroup.LayoutParams params = img.getLayoutParams();
                    params.width = ViewUtil.Dp2Px(mContext, designWidth);
                    params.height = ViewUtil.Dp2Px(mContext, designHeight);
                    img.setLayoutParams(params);
                }
                if (leftmargin > 0 || rightMargin > 0 || topMargin > 0 || bottomMargin > 0) {
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) img.getLayoutParams();
                    params.leftMargin = ViewUtil.Dp2Px(mContext, leftmargin);
                    params.rightMargin = ViewUtil.Dp2Px(mContext, rightMargin);
                    params.topMargin = ViewUtil.Dp2Px(mContext, topMargin);
                    params.bottomMargin = ViewUtil.Dp2Px(mContext, bottomMargin);
                    img.setLayoutParams(params);
                }
                Glide.with(mContext).load(s).override(designWidth, designHeight).into(img);
            }
        }
    }

}