Android:更改Fling Gesture事件上的选项卡 - 不适用于TabActivity中的ListActivity

时间:2011-09-26 10:02:22

标签: android listactivity gesture tabactivity

我有三个标签的tabactivity。其中一个选项卡包含ListActivity,另外两个包含简单的Activity。我已经实现了OnGestureListener来更改fling事件的选项卡。

没有为具有ListActivity的选项卡调用onTouchEvent和'onFling'事件。但是对于简单活动(没有列表)工作正常。

这是我的代码:

主要活动类:

public class GestureTest extends TabActivity implements OnGestureListener {
    private TabHost tabHost;
    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testmainlayoutforgesture);
        gestureScanner = new GestureDetector(this);
        addTabs();
    }

    private void addTabs() {
        Intent i1 = new Intent().setClass(this, SimpleListActivity.class);
        Intent i2 = new Intent().setClass(this, SimpleActivity.class);
        Intent i3 = new Intent().setClass(this, SimpleActivity.class);

        tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("First").setIndicator("First").setContent(i1));
        tabHost.addTab(tabHost.newTabSpec("Second").setIndicator("Second").setContent(i2));
        tabHost.addTab(tabHost.newTabSpec("Third").setIndicator("Third").setContent(i3));

        tabHost.setCurrentTab(0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        return gestureScanner.onTouchEvent(me);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float dispX = e2.getX() - e1.getX();
        float dispY = e2.getY() - e1.getY();
        if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 100) {
            // swipe ok
            if (dispX > 0) {
                // L-R swipe
                System.out.println("L-R swipe");
                changeLtoR();
            } else {
                // R-L swipe
                System.out.println("R-L swipe");
            }
        }
        return true;
    }

    private void changeLtoR() {
        int curTab = tabHost.getCurrentTab();
        int nextTab = ((curTab + 1) % 3);
        tabHost.setCurrentTab(nextTab);

    }
//other interface methods    
}

SimpleListActivity.java:

public class SimpleListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<String> list1Strings = new ArrayList<String>();
        list1Strings.add("List 11");
        list1Strings.add("List 12");
        list1Strings.add("List 13");
        list1Strings.add("List 14");
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
    }
}

SimpleActivity.java:

public class SimpleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simplelayouttest);
    }
}

testmainlayoutforgesture.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </FrameLayout>
    </LinearLayout>
</TabHost>

simplelayouttest.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/helloTv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello Hello" />
</LinearLayout>

我无法弄清楚这里有什么问题。

1 个答案:

答案 0 :(得分:6)

尝试将此添加到TabActivity:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (gestureScanner != null) {
        if (gestureScanner.onTouchEvent(ev))
            return true;
    }
    return super.dispatchTouchEvent(ev);
}

实现此功能,将告诉您的设备gestureScanner消耗了onFling事件。

再次编辑:

您还需要将onFling-method更改为:

    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;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, then dismiss the swipe.
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        // Swipe from right to left.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            // do stuff
            return true;
        }

        // Swipe from left to right.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            // do stuff
            return true;
        }

        return false;
    }
}    

另一种可能性是,如果您的ListView没有填满屏幕并且“空”空间不可点击,并且您在此“空”空间上投掷,则不会注册和分派触摸事件。如果你在ListView之上移动它是否有效?