TabSwitcher自定义控件(不带滑动效果)


这个是不带动画效果,要动画效果的看后面的文章。

public class TabSwitcher extends LinearLayout{

	private static final String tag="TabSwitcher";
	private Context context;
	private String[] texts;
	private int arrayId;
	private int selectedPosition=0;
	private int oldPosition=selectedPosition;
	private int[] background={R.drawable.tabswitcher_short,Color.TRANSPARENT};
	private TextView[] tvs;
	public TabSwitcher(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}
	public TabSwitcher(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		Log.i(tag, "--------------TabSwitcher2---------------------");
		init();
		TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.custom);  
		arrayId=a.getResourceId(R.styleable.custom_arrayId, 0);
		selectedPosition=a.getInt(R.styleable.custom_selectedPosition, 0);
        a.recycle();
	}
	private void init(){
		context=getContext();
		setOrientation(HORIZONTAL);
		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		setLayoutParams(params);
//		texts=new String[]{"游戏","应用","娱乐"};
		setBackgroundResource(R.drawable.tabswitcher_long);
		
	}
	
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		Log.i(tag, "--------------onFinishInflate---------------------");
		if(arrayId!=0){
			texts=getResources().getStringArray(arrayId);
		}else{
			texts=new String[]{};
		}
		
		tvs=new TextView[texts.length];
		LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		params.weight=1;
		params.gravity=Gravity.CENTER_VERTICAL;
		for(int i=0;i<texts.length;i++){
			TextView child=new TextView(context);
			child.setTag(i);
//			child.setText(texts[i]);
			child.setTextSize(16);
			child.setTextColor(Color.BLACK);
			child.setGravity(Gravity.CENTER);
			
			tvs[i]=child;
			child.setOnClickListener(listener);
			this.addView(child, params);
		}
	}

	OnClickListener listener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			selectedPosition=(Integer)v.getTag();
			if(selectedPosition!=oldPosition){
				tvs[oldPosition].setBackgroundColor(background[1]);
				oldPosition=selectedPosition;
				((TextView)v).setBackgroundResource(background[0]);
				if(onItemClickLisener!=null){
					onItemClickLisener.onItemClickLisener(v, selectedPosition);
				}
			}
		}
		
	};
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		Log.i("tag", "---------------onSizeChanged--------------------");
		if(selectedPosition>texts.length-1){
			throw new IllegalArgumentException("The selectedPosition can't be > texts.length.");
		}
		oldPosition=selectedPosition;
		for(int i=0;i<texts.length;i++){
			tvs[i].setText(texts[i]);
			if(selectedPosition==i){
				tvs[i].setBackgroundResource(background[0]);
			}else{
				tvs[i].setBackgroundColor(background[1]);
			}
		}
	}
	
	private OnItemClickLisener onItemClickLisener;
	public void setOnItemClickLisener(OnItemClickLisener onItemClickLisener) {
		this.onItemClickLisener = onItemClickLisener;
	}
	public interface OnItemClickLisener{
		void onItemClickLisener(View view,int position);
	}
	
	public void setTexts(String[] texts) {
		this.texts = texts;
	}
	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}
	
}



用法:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.yiyou.ui.view.TabSwitcher;
import com.yiyou.ui.view.TabSwitcher.OnItemClickLisener;

public class MainActivity extends Activity{
	private Context context;
	private TabSwitcher tabSwitcher;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context=this;
        tabSwitcher=(TabSwitcher)findViewById(R.id.tabSwitcher);
        tabSwitcher.setOnItemClickLisener(onItemClickLisener);
        
//        tabSwitcher.setTexts(new String[]{"1","2","3"});
        //tabSwitcher.setSelectedPosition(1);
    }
    
	
    OnItemClickLisener onItemClickLisener=new OnItemClickLisener(){

		@Override
		public void onItemClickLisener(View view, int position) {
			// TODO Auto-generated method stub
			//
			switch (position) {
			case 0:
			case 1:
			case 2:
				Toast.makeText(context, "position clicked:"+position, Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}
    	
    };
}


attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<declare-styleable name="custom">
		<attr name="arrayId" format="reference" />
            <attr name="selectedPosition" format="integer" />
	</declare-styleable>
</resources>


小屏幕的使用下面这张图片代替工程中的图片,否则图片会切掉!



Android Toggle Switch, a customizable extension of Android Switches
https://github.com/BelkaLab/Android-Toggle-Switch
  • 大小: 36.5 KB
  • 大小: 2.6 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值