package com.bncq.controller;

import java.util.ArrayList;
import java.util.List;
import com.bncq.R;
import com.bncq.common.UIUtil;
import com.bncq.model.TabFragmentAdapter;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class CaiquanFragment extends Fragment {
	//UI
	private ViewPager vp_cq_pager;//Ò³¿¨ÄÚÈÝ
	private List<Fragment> listViews; // TabÒ³ÃæÁÐ±í
	private ImageView ivw_cq_cursor;// ¶¯»­Í¼Æ¬
	private TextView tvw_cq_game, tvw_cq_rule, tvw_cq_result;// Ò³¿¨Í·±ê
	//±äÁ¿
	private List<TextView> listTextViews;//±êÌâÁÐ±í
	private int offset = 0;// ¶¯»­Í¼Æ¬Æ«ÒÆÁ¿
	private int bmpW;// ¶¯»­Í¼Æ¬¿í¶È
	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
		View caiquanLayout=inflater.inflate(R.layout.caiquan_layout,container,false);
		vp_cq_pager = (ViewPager) caiquanLayout.findViewById(R.id.vp_cq_pager);
		ivw_cq_cursor = (ImageView) caiquanLayout.findViewById(R.id.ivw_cq_cursor);
		//±äÁ¿
		/**
		 * ³õÊ¼»¯Í·±ê
		 */
		tvw_cq_game = (TextView) caiquanLayout.findViewById(R.id.tvw_cq_game);
		tvw_cq_rule = (TextView)caiquanLayout. findViewById(R.id.tvw_cq_rule);
		tvw_cq_result = (TextView)caiquanLayout. findViewById(R.id.tvw_cq_result);
		listTextViews=new ArrayList<TextView>();
		listTextViews.add(tvw_cq_game);
		listTextViews.add(tvw_cq_rule);
		listTextViews.add(tvw_cq_result);
		tvw_cq_game.setOnClickListener(new UIUtil.TabOnClickListener(0, vp_cq_pager,listTextViews));
		tvw_cq_rule.setOnClickListener(new UIUtil.TabOnClickListener(1, vp_cq_pager,listTextViews));
		tvw_cq_result.setOnClickListener(new UIUtil.TabOnClickListener(2, vp_cq_pager,listTextViews));

		/**
		 * ³õÊ¼»¯¶¯»­
		 */
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.tab_cursor).getWidth();// »ñÈ¡Í¼Æ¬¿í¶È
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// »ñÈ¡·Ö±æÂÊ¿í¶È
		offset = (screenW / 3 - bmpW) / 2;// ¼ÆËãÆ«ÒÆÁ¿
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		ivw_cq_cursor.setImageMatrix(matrix);// ÉèÖÃ¶¯»­³õÊ¼Î»ÖÃ
		/**
		 * ³õÊ¼»¯ViewPager
		 */
		vp_cq_pager.setOffscreenPageLimit(1);
		listViews = new ArrayList<Fragment>();
		listViews.add(new CaiquanGameFragment());
		listViews.add(new CaiquanRuleFragment());
		listViews.add(new CaiquanResultFragment());
		vp_cq_pager.setAdapter(new TabFragmentAdapter(getChildFragmentManager(),listViews));
		vp_cq_pager.setCurrentItem(0);
		vp_cq_pager.setOnPageChangeListener(new UIUtil.TabOnPageChangeListener(offset, bmpW, ivw_cq_cursor,listTextViews));
		return caiquanLayout;
	}
}
 
