我的tabHost有三欄,都是listView,想加一個滑動切換tab的效果,結果發現不行,於是
用了個簡單的來測試,發現如下問題。
情況一
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
情況二
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ems="10" />
</LinearLayout>
這個tab裡面只有這個EditText,就android:layout_height不同
然後就發現滑動效果在fill_parent的時候不反應了!
這是什麼問題???
下面是ACtivity的內容
package com.mine.tabhostdemo2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabHostActivity extends FragmentActivity {
private static TabHost tabHost;
private GestureDetector detector=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host);
// tabHost = (TabHost) findViewById(R.id.tabhost);
init();
if (detector==null){
detector= new GestureDetector(this,
new MySimpleOnGestureListener());
}
}
private void init() {
// TODO Auto-generated method stub
tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabSpec spec1 = tabHost.newTabSpec("1");
spec1.setIndicator("1",
getResources().getDrawable(R.drawable.ic_launcher));
spec1.setContent(R.id.tab1);
TabSpec spec2 = tabHost.newTabSpec("2");
spec2.setIndicator("2",
getResources().getDrawable(R.drawable.ic_launcher));
spec2.setContent(R.id.tab2);
TabSpec spec3 = tabHost.newTabSpec("3");
spec3.setIndicator("3",
getResources().getDrawable(R.drawable.ic_launcher));
spec3.setContent(R.id.tab3);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
//method one
/*detector = new GestureDetector(this,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
if ((e2.getRawX() - e1.getRawX()) > 80) {
showNext();
return true;
}
if ((e1.getRawX() - e2.getRawX()) > 80) {
showPre();
return true;
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});*/
}
//method two
private static class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener {
public MySimpleOnGestureListener() {
super();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if ((e2.getRawX() - e1.getRawX()) > 80) {
showNext();
return true;
}
if ((e1.getRawX() - e2.getRawX()) > 80) {
showPre();
return true;
}
//Log.e(TAG, "onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)");
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(detector!=null){
detector.onTouchEvent(event);
}else{
Log.i("nimeide1", "no detector!!!!!!!!!!!!");
}
return super.onTouchEvent(event);
}
static int i = 0;
protected static void showPre() {
// TODO Auto-generated method stub
tabHost.setCurrentTab(i = i == 2 ? i = 0 : ++i);
Log.i("kennet", i + "");
}
protected static void showNext() {
// TODO Auto-generated method stub
tabHost.setCurrentTab(i = i == 0 ? i = 2 : --i);
Log.i("kennet", i + "");
}
}
已經找到解決方法了
解決touch事件的沖突