package com.wasu.cs.widget;

import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.AbsListView;
import cn.com.wasu.main.R;

import com.wasu.widget.FocusGridView;

public class CustomScrollGridView extends FocusGridView {

	public CustomScrollGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		resetScrollBar();
		
		setContentDescription("内容信息列表");
	}

	@SuppressLint("NewApi")
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		
		if (getAdapter() != null && getChildCount() < getCount()) {
			setFastScrollAlwaysVisible(true);
		}else{
			setFastScrollAlwaysVisible(false);
		}
	}
	
	/**
	 * 修改scrollbar样式
	 */
	@SuppressLint("NewApi")
	private void resetScrollBar() {
		try {
			Field mFastScroller = AbsListView.class.getDeclaredField("mFastScroller");
			mFastScroller.setAccessible(true);
			Object o = mFastScroller.get(this);
			if (Build.VERSION.SDK_INT < 19) { //4.4以下
				// 修改透明度
//				Field mScrollFade = mFastScroller.getType().getDeclaredField("mScrollFade");
//				mScrollFade.setAccessible(true);
//				Object scrollFadeObj = mScrollFade.get(o);
//				Class<?>[] classes = mScrollFade.getDeclaringClass().getDeclaredClasses();
//				for (Class<?> clazz : classes) {
//					if (clazz.getName().contains("ScrollFade")) {
//						Field alphaMax = clazz.getDeclaredField("ALPHA_MAX");
//						alphaMax.setAccessible(true);
//						alphaMax.set(scrollFadeObj, 0);
//					}
//				}
				// 滑动指示器
				Field mThumbDrawable = mFastScroller.getType().getDeclaredField("mThumbDrawable");
				mThumbDrawable.setAccessible(true);
				Drawable thumbDrawable = (Drawable) mThumbDrawable.get(o);
				thumbDrawable = getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_thumb);
				mThumbDrawable.set(o, thumbDrawable);
				// 滑动指示器宽度
				Field mThumbWidth = mFastScroller.getType().getDeclaredField("mThumbW");
				mThumbWidth.setAccessible(true);
				int thumbWidth = (Integer) mThumbWidth.get(o);
				thumbWidth = getResources().getDimensionPixelSize(R.dimen.d_10dp);
				mThumbWidth.set(o, thumbWidth);
				//滑动指示器高度
				Field mThumbHeight = mFastScroller.getType().getDeclaredField("mThumbH");
				mThumbHeight.setAccessible(true);
				int thumbHeight = (Integer) mThumbHeight.get(o);
				thumbHeight = getResources().getDimensionPixelSize(R.dimen.d_90dp);;
				mThumbHeight.set(o, thumbHeight);
				// 背景
//				Field mTrackDrawable = mFastScroller.getType().getDeclaredField("mTrackDrawable");
//				mTrackDrawable.setAccessible(true);
//				Drawable trackDrawable = (Drawable) mTrackDrawable.get(o);
//				trackDrawable = getResources().getDrawable(R.drawable.scrollbar_vertical_track);
//				mTrackDrawable.set(o, trackDrawable);
			} else {
				// 滑动指示器
//				Field mThumbImage = mFastScroller.getType().getDeclaredField("mThumbImage");
//				mThumbImage.setAccessible(true);
//				ImageView thumbImage = (ImageView) mThumbImage.get(o);
//				thumbImage.setBackground(getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_thumb));
//				mThumbImage.set(o, thumbImage);
//				thumbImage.setMinimumWidth(R.dimen.d_10dp);
//				thumbImage.setMinimumHeight(R.dimen.d_90dp);
				
//				// 背景
//				Field mTrackImage = mFastScroller.getType().getDeclaredField("mTrackImage");
//				mTrackImage.setAccessible(true);
//				ImageView trackImage = (ImageView) mTrackImage.get(o);
//				trackImage.setBackground(getResources().getDrawable(R.drawable.scrollbar_drawable_vertical_track));
//				mTrackImage.set(o, trackImage);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
