我一直在尝试将 onFling Gesture 添加到由 TabHost 组成的 Activity 中。
我用 TabWidget 构建了我的标签使用 xml。这是我的代码
活动1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/establecimientoView">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent" >
<LinearLayout
android:id="@+id/tabIdEst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/identificacion_establecimiento"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tabRefGeo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/referencia_geografica"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tabUbicEstablecimiento"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/ubicacion_establecimiento"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tabVialidades"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/entre_vialidades"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Activity1.java
public class EstablecimientoActivity extends Activity implements GestureDetector.OnGestureListener{
TextView lblE90;
TextView lblExxGastos;
TabHost tabs;
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 mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.establecimiento);
mGestureDetector = new GestureDetector(this, this, null);
initTabGroup();
View establecimientoView = findViewById(R.id.establecimientoView);
establecimientoView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
Log.d("On touch", "view establecimiento");
return true;
}
return false;
}
});
}
private void initTabGroup(){
tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tabIdEst");
spec.setContent(R.id.tabIdEst);
spec.setIndicator("Identificación del Establecimiento");
tabs.addTab(spec);
spec=tabs.newTabSpec("tabRefGeo");
spec.setContent(R.id.tabRefGeo);
spec.setIndicator("Referencia Geográfica");
tabs.addTab(spec);
spec=tabs.newTabSpec("tabUbEst");
spec.setContent(R.id.tabUbicEstablecimiento);
spec.setIndicator("Ubicación Establecimiento");
tabs.addTab(spec);
spec=tabs.newTabSpec("tabVialidades");
spec.setContent(R.id.tabVialidades);
spec.setIndicator("Entre Vialidades");
tabs.addTab(spec);
tabs.setCurrentTab(0);
}
// Listeners
@Override
public boolean onDown(MotionEvent arg0) {
Log.d("onDown", "Tap");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d("onFling", e1.toString());
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("onFling","right to left");
Intent intent = new Intent(this.getBaseContext(),
Activity2.class);
startActivity(intent);
this.overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
// left to right swipe
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("onFling","left to right");
this.overridePendingTransition(R.anim.slide_in_left,
R.anim.slide_out_right);
}
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
}
我一直在做一些研究,但我只知道标签之间的滑动方式。我只是想去另一个活动,我的意思是我想从 Activity1(包含 tabhost)去 Activity2
我做错了什么?