package com.stx.openeyes.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import org.xutils.x;
/**
* Created by xhb on 2016/2/20.
* 通用的ViewHolder工具类
*/
public class ViewHolder {
//使用SparseArray是因为当键值对映射是 Integer--Object 时 SparseArray的效率要比Map高
private SparseArray<View> mViews;
private View mConvertView;
private int mPosition;
private Context mContext;
private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
mContext=context;
this.mViews = new SparseArray<>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
//设置tag
mConvertView.setTag(this);
}
/**
* 拿到一个ViewHolder对象
*
* @param context 上下文
* @param convertView 缓存布局
* @param parent 父控件
* @param layoutId 布局id
* @param position 位置
* @return
*/
public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
}else {
ViewHolder holder = (ViewHolder) convertView.getTag();
//更新position
holder.mPosition=position;
return holder;
}
}
/**
* 通过控件的viewId获取对应的控件,如果没有则加入views
*
* @param viewId
* @param <T>
* @return
*/
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mConvertView;
}
/**
* 给TextView设置值
* @param viewId 控件id
* @param text 值
* @return
*/
public ViewHolder setText(int viewId,String text){
TextView tv =getView(viewId);
tv.setText(text);
return this;
}
/**
* 给TextView设置字体颜色
* @param viewId
* @param textColor
* @return
*/
public ViewHolder setTextColor(int viewId, int textColor)
{
TextView view = getView(viewId);
view.setTextColor(textColor);
return this;
}
/**
* 给TextView设置颜色
* @param viewId
* @param textColorRes 颜色id
* @return
*/
public ViewHolder setTextColorRes(int viewId, int textColorRes)
{
TextView view = getView(viewId);
view.setTextColor(mContext.getResources().getColor(textColorRes));
return this;
}
/**
* 给TextView设置链接
* @param viewId
* @return
*/
public ViewHolder linkify(int viewId)
{
TextView view = getView(viewId);
Linkify.addLinks(view, Linkify.ALL);
return this;
}
/**
* 给ImageView设置ImageResource
* @param viewId 控件id
* @param resId 资源id
* @return
*/
public ViewHolder setImageResource(int viewId,int resId){
ImageView iv= getView(viewId);
iv.setImageResource(resId);
return this;
}
/**
* 给ImageView设置ImageResource使用Frsesco
* @param uri 资源uri
* @return
*/
public ViewHolder setImageResourcewithFresco(int viewId,Uri uri){
SimpleDraweeView draweeView=getView(viewId);
draweeView.setImageURI(uri);
return this;
}
/**
* 给ImageView设置bitmap
* @param viewId
* @param bitmap
* @return
*/
public ViewHolder setImageBitmap(int viewId,Bitmap bitmap){
ImageView iv= getView(viewId);
iv.setImageBitmap(bitmap);
return this;
}
/**
* 加载网络图片使用xutils
* @param viewId
* @param uri
* @return
*/
public ViewHolder setImageURIWithXutils(int viewId,String uri){
ImageView iv= getView(viewId);
x.image().bind(iv,uri);
return this;
}
/**
* 给ImageView设置drawable
* @param viewId
* @param drawable
* @return
*/
public ViewHolder setImageDrawable(int viewId, Drawable drawable)
{
ImageView view = getView(viewId);
view.setImageDrawable(drawable);
return this;
}
/**
* 给控件设置背景颜色
* @param viewId
* @param color
* @return
*/
public ViewHolder setBackgroundColor(int viewId, int color)
{
View view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
/**
* 给控件设置背景图片
* @param viewId
* @param backgroundRes
* @return
*/
public ViewHolder setBackgroundRes(int viewId, int backgroundRes)
{
View view = getView(viewId);
view.setBackgroundResource(backgroundRes);
return this;
}
/**
* 设置透明度
* @param viewId
* @param value
* @return
*/
@SuppressLint("NewApi")
public ViewHolder setAlpha(int viewId, float value)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getView(viewId).setAlpha(value);
} else
{
// Pre-honeycomb hack to set Alpha value
AlphaAnimation alpha = new AlphaAnimation(value, value);
alpha.setDuration(0);
alpha.setFillAfter(true);
getView(viewId).startAnimation(alpha);
}
return this;
}
/**
* 设置控件是否可见
* @param viewId
* @param visible
* @return
*/
public ViewHolder setVisible(int viewId, boolean visible)
{
View view = getView(viewId);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
return this;
}
/**
*设置字体类型
* @param typeface
* @param viewIds
* @return
*/
public ViewHolder setTypeface(Typeface typeface, int... viewIds)
{
for (int viewId : viewIds)
{
TextView view = getView(viewId);
view.setTypeface(typeface);
view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
return this;
}
/**
*点击事件
*/
public ViewHolder setOnClickListener(int viewId,
View.OnClickListener listener)
{
View view = getView(viewId);
view.setOnClickListener(listener);
return this;
}
/**
* 设置进度条
* @param viewId
* @param progress
* @return
*/
public ViewHolder setProgress(int viewId, int progress)
{
ProgressBar view = getView(viewId);
view.setProgress(progress);
return this;
}
/**
* 设置进度条
* @param viewId
* @param progress
* @param max
* @return
*/
public ViewHolder setProgress(int viewId, int progress, int max)
{
ProgressBar view = getView(viewId);
view.setMax(max);
view.setProgress(progress);
return this;
}
public ViewHolder setMax(int viewId, int max)
{
ProgressBar view = getView(viewId);
view.setMax(max);
return this;
}
public ViewHolder setRating(int viewId, float rating)
{
RatingBar view = getView(viewId);
view.setRating(rating);
return this;
}
public ViewHolder setRating(int viewId, float rating, int max)
{
RatingBar view = getView(viewId);
view.setMax(max);
view.setRating(rating);
return this;
}
/**
* 设置标签
* @param viewId
* @param tag
* @return
*/
public ViewHolder setTag(int viewId, Object tag)
{
View view = getView(viewId);
view.setTag(tag);
return this;
}
/**
* 设置标签
* @param viewId
* @param key
* @param tag
* @return
*/
public ViewHolder setTag(int viewId, int key, Object tag)
{
View view = getView(viewId);
view.setTag(key, tag);
return this;
}
/**
* 设置是否选中
* @param viewId
* @param checked
* @return
*/
public ViewHolder setChecked(int viewId, boolean checked)
{
Checkable view = (Checkable) getView(viewId);
view.setChecked(checked);
return this;
}
/**
* 触摸事件
* @param viewId
* @param listener
* @return
*/
public ViewHolder setOnTouchListener(int viewId,
View.OnTouchListener listener)
{
View view = getView(viewId);
view.setOnTouchListener(listener);
return this;
}
/**
* 长按事件
* @param viewId
* @param listener
* @return
*/
public ViewHolder setOnLongClickListener(int viewId,
View.OnLongClickListener listener)
{
View view = getView(viewId);
view.setOnLongClickListener(listener);
return this;
}
}