android tabhost的用法

android  tabhost的用法之setContent().

在安卓中setContent有三种方式:

TabSpec setContent(int viewId);

TabSpec setContent(TabContentFactory contentFactory);

TabSpec setContent(Intent intent);

我下面的代码实例实现的是第二种方式。第一种方式相对简单,不做讲述。

第三种方式与第二种方式类似,在编码过程中注意:the view already  is a child of parent的类似错误

       


package com.example.navigation;



import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
//public class MainActivity extends TabActivity implements OnGestureListener
public class MainActivity extends Activity implements OnGestureListener,TabContentFactory{
    private  static int SCREEN_WIDTH;
TabHost tabhost;
TabWidget widget;
GestureDetector detector;
HorizontalScrollView scrollview;

String currentID="tab01";
int[] viewId=new int[]{R.id.tab01,R.id.tab02,R.id.tab03};
String []title={"我的","推荐","排行"};
String[]tags={"tab01","tab02","tab03"};
boolean flag=false;
boolean isRight=false;//是否向右划动
boolean isScroll=false;//是否滑动
float startXPosition=0;
int scrollmax;
float lastrate=0;
FrameLayout framelayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);
WindowManager wm=getWindowManager();
DisplayMetrics metrics=new DisplayMetrics();
Display display=wm.getDefaultDisplay();
display.getMetrics(metrics);



SCREEN_WIDTH=metrics.widthPixels;

scrollview=(HorizontalScrollView) findViewById(R.id.scrollview);

tabhost=(TabHost) findViewById(R.id.tabhost);
tabhost.setup();

framelayout=tabhost.getTabContentView();
先设置framelayout中的view,否则framelayout中没有内容

for(int i=0;i<tags.length;i++){
只执行一次 createTabContent,即初始化时只调用一次,其余的在第一次点击标签时初始化
ViewGroup tmp=(ViewGroup) LayoutInflater.from(MainActivity.this).inflate(R.layout.tabcontent, null);
View vvv=(View)tmp.findViewById(viewId[i]);
一个View不能处于多个父容器中,需要先从之前一个父容器中删除
tmp.removeView(vvv);
framelayout.addView(vvv);
///不显示的隐藏掉
vvv.setVisibility(View.GONE);
}

for(int i=0;i<title.length;i++){
TabSpec tabspec=tabhost.newTabSpec(tags[i]);
tabspec.setContent(this);
View v=LayoutInflater.from(MainActivity.this).inflate(R.layout.custom, null);
tabspec.setIndicator(v);
tabhost.addTab(tabspec);
}
detector=new GestureDetector(this, this);
tabhost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {
View view=null;
Log.e("tag", "onchange");
for(int i=0;i<tags.length;i++){
if(tabId.equals(tags[i])){
view=framelayout.getChildAt(i);
break;
}
}
currentID=tabId;
Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alphaanimation);
view.setAnimation(animation);
animation.start();
}

});
}

@Override
public View createTabContent(String tag) {
View vv=null;
for(int i=0;i<tags.length;i++){
if(tag.equals(tags[i])){
Log.e("tag",tag);
vv=framelayout.getChildAt(i);
break;
}
}

return vv;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_UP){
return super.onTouchEvent(event);
}else{
return detector.onTouchEvent(event);
}

}


@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
flag=true;
return false;
}


@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if(flag){
if(e1.getX()>e2.getX()){
if(tabhost.getCurrentTab()==tags.length-1){
tabhost.setCurrentTab(0);
}else{
tabhost.setCurrentTab(tabhost.getCurrentTab()+1);
}
}else if(e1.getX()<e2.getX()){
if(tabhost.getCurrentTab()==0){
tabhost.setCurrentTab(tags.length-1);
}else{
tabhost.setCurrentTab(tabhost.getCurrentTab()-1);
}
}

isScroll=true;
flag=false;
}
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}

}


转载请注明地址,谢谢!源码下载地址 http://download.csdn.net/detail/chengjiamei/8038379  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

释汐宇辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值