Android TabHost 选项卡 滑动activity进行切换选项卡

1、布局界面:



    

        

            

            
            
        
    

2、tab选项卡布局




    

    

        
    

3、主activity的代码:

public class TestActivity extends TabActivity  {
	protected TabHost tabHost;
	private static final String TAB_PAGE1="page1";
	private static final String TAB_PAGE2="page2";
	private static final String TAB_PAGE3="page3";
	private static final String TAB_PAGE4="page4";
	
	private int tab_last_position;
	//通过TabWidget可以获取选项卡的布局
	private TabWidget mTabWidget;
	 
	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private GestureDetector gestureDetector;
	View.OnTouchListener gestureListener;
	 
	private static int maxTabIndex = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apply_main);
        
        tabHost = getTabHost();
       
        TabSpec tabSpec = tabHost.newTabSpec(TAB_PAGE1);
        tabSpec.setIndicator(createTabView("音乐"));
        Intent intent = new Intent(this, GovernmentActivity.class);
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec(TAB_PAGE2);
        intent = new Intent(this, ClassifyActivity.class);
        tabSpec.setContent(intent);
        tabSpec.setIndicator(createTabView("电影"));
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec(TAB_PAGE3);
        tabSpec.setIndicator(createTabView("电视剧"));
        intent = new Intent(this, PaihangActivity.class);
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);
        //
        tabSpec = tabHost.newTabSpec(TAB_PAGE4);
        tabSpec.setIndicator(createTabView("书籍"));
        intent = new Intent(this, FirstTestActivity.class);
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);
        //默认使其选中
        tabHost.setCurrentTab(0);
        tab_last_position=0;
        View view =  tabHost.getCurrentTabView();
		ImageView imageView = (ImageView) view.findViewById(R.id.tab_line);
		TextView mTextView = (TextView) view.findViewById(R.id.tab_name);
		imageView.setVisibility(View.VISIBLE);
		mTextView.setTextColor(Color.GREEN);
		mTabWidget = tabHost.getTabWidget();
		
		 
		
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				ImageView imageView = null;
				Logger.getLogger().i("------>" + tab_last_position);
				//获取到视图,可以对其进行样式的改变
				View lastView = mTabWidget.getChildAt(tab_last_position);
				imageView = (ImageView) lastView.findViewById(R.id.tab_line);
				TextView mTextView = (TextView) lastView.findViewById(R.id.tab_name);
				if (imageView!=null) {
					imageView.setVisibility(View.GONE);
					mTextView.setTextColor(getResources().getColor(R.color.white));
				}
				
				View view =  tabHost.getCurrentTabView();
				imageView = (ImageView) view.findViewById(R.id.tab_line);
				mTextView = (TextView) view.findViewById(R.id.tab_name);
				imageView.setVisibility(View.VISIBLE);
				mTextView.setTextColor(Color.GREEN);
				
				mTabWidget = tabHost.getTabWidget();
				tab_last_position = tabHost.getCurrentTab();
				 
			}
		});
        
        
        //滑动事件
        gestureDetector = new GestureDetector(new TabGestureDetector());  
//        gestureListener = new View.OnTouchListener() {  
//            public boolean onTouch(View v, MotionEvent event) {  
//                if (gestureDetector.onTouchEvent(event)) {  
//                    return true;  
//                }  
//                return false;  
//            }  
//        };  
        
    }
    
    // 创建tab标签
    protected View createTabView(String name) {
        View tabView = getLayoutInflater().inflate(R.layout.tab_background, null);
        TextView textView = (TextView) tabView.findViewById(R.id.tab_name);// 找到textview控件
        textView.setText(name);
        return tabView;
    }
    
    
    // 左右滑动刚好页面也有滑动效果  
    private class TabGestureDetector extends SimpleOnGestureListener {  
        @Override  
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                float velocityY) {  
           
            try {  
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)  
                    return false;  
                // right to left swipe  
                int currentView = tabHost.getCurrentTab();
                
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE  
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
                    Log.i("test", "right" +currentView);  
                    if (currentView SWIPE_MIN_DISTANCE  
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
                    Log.i("test", "left" +currentView);  
                    if (currentView > 0) {  
                    	 currentView--;  
                    	 tabHost.setCurrentTab(currentView);  
                    }
                    
                }  
            } catch (Exception e) {  
            	e.printStackTrace();
            }  
            return false;  
        }  
    }  
  
    @Override  
    public boolean dispatchTouchEvent(MotionEvent event) {  
        if (gestureDetector.onTouchEvent(event)) {  
        	Log.i("test", "---------------dispatch ... ");  
            event.setAction(MotionEvent.ACTION_CANCEL);  
        }  
        return super.dispatchTouchEvent(event);  
    }  
     
}



你可能感兴趣的:(Android,进阶,tabhost,滑动选项卡,activity,GestureDetector)