タブを画面のフリック動作で切り替える方法は下記のソースの通り。
画面レイアウトファイルにTabHostを設置している想定
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//タブの設定
tabs = (TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabSpec spec = tabs.newTabSpec("tab1");
spec.setIndicator("tab1");
spec.setContent( R.id.tab1);
tabs.addTab( spec );
spec = tabs.newTabSpec("tab2");
spec.setIndicator("tab2");
spec.setContent( R.id.tab2);
tabs.addTab( spec );
tabs.setCurrentTab(0);
}
//フリック動作によるタブの切替
protected float lastTouchX;
protected float limitx = 100;
public boolean onTouchEvent( MotionEvent event ){
switch( event.getAction() ){
case MotionEvent.ACTION_DOWN:
lastTouchX = event.getX();
break;
case MotionEvent.ACTION_UP:
float diff = lastTouchX - event.getX();
if( diff > limitx ){
tabs.setCurrentTab(0);
return true;
}
if( diff < -1 * limitx ){
tabs.setCurrentTab(1);
return true;
}
break;
}
return super.onTouchEvent(event);
}