package cn.madfinger.android.core.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
/**
* Tab栏 控件
* @author wiley.wang
*/
public class TabHostView extends LinearLayout {
private int mCount;
private View tabViews[];
private int mSelected = -1;
public TabHostView(Context context) {
super(context);
}
public TabHostView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
mCount = this.getChildCount();
tabViews = new View[mCount];
for (int i = 0; i < mCount; i++) {
tabViews[i] = this.getChildAt(i);
tabViews[i].setOnClickListener(clickEvent);
}
}
private View.OnClickListener clickEvent = new OnClickListener() {
@Override
public void onClick(View v) {
int index = -1;
for (int i = 0; i < mCount; i++) {
if (tabViews[i] == v) {
index = i;
break;
}
}
if (null == onTabChangedListener)
return;
if (onTabChangedListener.onClick(index)) {// 执行回调事件
setFocus(index);// 回调成功后切换焦点
}
}
};
public void onClickTab(int index) {
if(index<0&&index>=mCount)return;
if(onTabChangedListener.onClick(index)) {
setFocus(index);
}
}
private void setFocus(int index) {
if (index >= mCount || index == mSelected)
return;
if (mSelected > -1) {
if (tabViews[mSelected] instanceof ViewGroup) {
ViewGroup group = ((ViewGroup) tabViews[mSelected]);
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
group.getChildAt(i).setSelected(false);
}
} else {
tabViews[mSelected].setSelected(false);
}
}
View tabView = tabViews[index];
if (tabView instanceof ViewGroup) {
ViewGroup group = ((ViewGroup) tabView);
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
group.getChildAt(i).setSelected(true);
}
} else {
tabView.setSelected(true);
}
mSelected = index;
}
private onTabChangedListener onTabChangedListener;
public void setOnTabChangedListener(onTabChangedListener l) {
onTabChangedListener = l;
}
public interface onTabChangedListener {
boolean onClick(int index);
}
}
<?xml version="1.0" encoding="utf-8"?> <!-- --> <cn.madfinger.android.core.view.TabHostView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_height="60dip" android:layout_width="fill_parent" > <RelativeLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:layout_gravity="center_vertical" android:layout_margin="2dip" > <ImageView android:id="@+id/tab1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_centerInParent="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:layout_gravity="center_vertical" android:layout_margin="2dip" > <ImageView android:id="@+id/tab2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_centerInParent="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:layout_gravity="center_vertical" android:layout_margin="2dip" > <ImageView android:id="@+id/tab3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_centerInParent="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/layout4" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:layout_gravity="center_vertical" android:layout_margin="2dip" > <ImageView android:id="@+id/tab4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_centerInParent="true" /> </RelativeLayout> </cn.madfinger.android.core.view.TabHostView>
package cn.company.android.project.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import android.widget.ViewFlipper;
import cn.company.android.project.R;
import cn.company.android.project.util.Constants;
import cn.madfinger.android.core.view.TabHostView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private static String TAG = MainActivity.class.getSimpleName();
private Context mContext=null;
private MyApplication mApplication = null;
private TabHostView mTabHost = null;
private ViewFlipper mViewFlipper = null;
private Animation mInLeftAnim;
private Animation mOutLeftAnim;
private Animation mInRightAnim;
private Animation mOutRightAnim;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mApplication = (MyApplication) this.getApplication();
mContext=this;
initCtrl();
}
private void initCtrl() {
//初始化TabHost
mTabHost = (TabHostView) findViewById(R.id.tabhost);
mTabHost.setOnTabChangedListener(onTabChangedListener);
//初始化ViewFlipper
mViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
//初始化按钮
// 初始化动画
mInLeftAnim = AnimationUtils.loadAnimation(this, R.anim.anim_in_left);
mOutLeftAnim = AnimationUtils.loadAnimation(this, R.anim.anim_out_left);
mInRightAnim = AnimationUtils.loadAnimation(this, R.anim.anim_in_right);
mOutRightAnim = AnimationUtils.loadAnimation(this,
R.anim.anim_out_right);
}
//切换TAB
public void setFocusTab(int index) {
mTabHost.onClickTab(index);
}
TabHostView.onTabChangedListener onTabChangedListener = new TabHostView.onTabChangedListener() {
@Override
public boolean onClick(int index) {//tabIndex
Toast.makeText(mContext, ""+index, Toast.LENGTH_SHORT).show();
return true;
}
};
// 切换切图时动画效果
public void changeViewAnimation(int index, boolean anim) {
}
@Override
protected void onResume() {
super.onResume();
}
// 切回Activity时执行
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
int funId = data.getIntExtra(Constants.FUNCTION_KEY, 0);
Log.d(TAG, "onActivityResult:RESULT_OK");
Log.d(TAG, "FUNCTION:" + funId);
switch (funId) {
case Constants.FUNCTION_1:
break;
case Constants.FUNCTION_2:
break;
case Constants.FUNCTION_3:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
// 按键时执行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 实现按健监听
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("确认退出吗?").setIcon(
android.R.drawable.ic_menu_help).setCancelable(false)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// 退出
finish(); //
System.exit(0);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
}