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);
}
}
分享到:
相关推荐
自定义Android中TabHost组件显示在屏幕底部,并实现滑动切换页面。在此基础上可以实现类似米聊等软件主界面的Tab效果。
自定义 Tabhost 通过ActivityGroup实现Tabhost 值得研究!
如果继承TabActivity,并且通过getTabHost()方法来获取TabHost,那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent,如果继承Activity,可以通过findViewById来获取这三个组件,...
android自定义RadioButton,可用于竖直方向的tabhost组件的自定义设计,实现了tabWiget的效果。
android组件之 ratingBar processbar listview tabhost seekbar gallery ImageSwitcher 大集合,并且ratingBar的背景图片自定义
Android UI设计技巧,tabHost的用法、listview美化、控件的美化、自定义组件、自定义属性、自定义图形等!
并且通过getTabHost()方法来获取TabHost,那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent,如果继承Activity,可以通过findViewById来获取这三个组件,此时ID可自定义。...
选项卡 TabHost六.滚动视图 ScrollView七.自动完成文本框 AutoCompleteTextView八. 题目记录 零. 适配器Adapter 将各种数据以合适的形式显示在View中给用户看。 ArrayAdapter 支持泛型操作,最简单的一个Adapter,...
项目里包含了登陆注册的实现、音乐的识别播放暂停等功能、数据库的创建及使用(SQLite)、自定义适配器、activity、service、intent、broadcastreceiver四大组件以tabhost等一系列控件。 - 项目介绍 运行中有什么...
|--TabHost一个界面显示多Activity |--TextView单行跑马灯效果 |--TextView虚拟获得焦点 |--uploadServlet |--uri之表示资源resource |--ViewPage的使用 |--view中的tag用法之存储对象 |--view常用属性 |--xml常用...
TabHost 79 ProgressBar、SeekBar、RatingBar 81 进度条(ProgressBar) 81 拖动条(SeekBar) 83 评分组件(RatingBar) 86 GridView、Gallery和ImageSwitcher 88 GridView 88 Gallery和ImageSwitcher 91 ListView 94 一...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView)的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 ...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...
2.4.7 选项卡(TabHost)的功能和 用法 93 2.4.8 滚动视图(ScrollView) 的 功能和用法 95 2.4.9 列表视图(ListView和 ListActivity) 95 2.4.10 可展开的列表组件(ExpandableListView) 101 2.4.11 网格视图...
视频名称 源码 ----------------------...复杂UI界面设计、GPS与GoogleMap、自定义View、Ubuntu Linux下使用C语言面向底层开发、通过JNI进行底层组件调用、图形与OpenGl ES、界面特效、下载修改及编绎Android框架代码。
视频名称 源码 ----------------------...复杂UI界面设计、GPS与GoogleMap、自定义View、Ubuntu Linux下使用C语言面向底层开发、通过JNI进行底层组件调用、图形与OpenGl ES、界面特效、下载修改及编绎Android框架代码。
7.3.2 自定义TabHost 7.3.3 使用对话框 7.3.4使用滑动抽屉 7.4 小结 第3篇功能实现 第8章Android应用程序组成 8.1深入理解Activity 8.1.1使用Intent连接Activity 8.1.2 Activity的生命周期 …… 第9章Android中的...
7.3.2 自定义TabHost 7.3.3 使用对话框 7.3.4使用滑动抽屉 7.4 小结 第3篇功能实现 第8章Android应用程序组成 8.1深入理解Activity 8.1.1使用Intent连接Activity 8.1.2 Activity的生命周期 …… 第9章Android中的...