Android:更改 Fling 手势事件上的选项卡 - 不适用于 TabActivity 内的 ListActivity

发布于 2024-12-06 12:12:35 字数 4487 浏览 1 评论 0原文

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

使用 ListActivity 不会为选项卡调用 onTouchEvent 和 'onFling' 事件。但对于简单活动(没有列表)工作得很好。

这是我的代码:

Main Activity 类:

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>

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

I have a tabactivity with three tabs. One of the tab contains ListActivity and two others contains simple Activity. And i have implemented OnGestureListener to change the tab on fling event.

The onTouchEvent and 'onFling' events are not being called for the tab with ListActivity . But working fine for Simple Activity( without lists).

Here is my Code :

Main Activity class :

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 layout :

<?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 layout :

<?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>

I could not figure out what is wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-12-13 12:12:35

尝试将其添加到您的 TabActivity 中:

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

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

再次编辑:

您还需要将您的 onFling 方法稍微更改为:

    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 上滑动,它会起作用吗?

Try adding this to your TabActivity:

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

Implementing this, will tell your device that the gestureScanner consumed the onFling event.

EDIT Again:

You will also need to change your onFling-method a bit to this:

    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;
    }
}    

Another possibility could be if your ListView does not fill the screen and the 'empty' space is not clickable and you fling on this 'empty' space the touch event won't be registered and dispatched. Does it work if you fling on top of the ListView?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文