0

我想使用 viewSwitcher 在两种布局之间切换。它可以使用按钮,但我想通过触摸屏幕进行切换。我找到了一个 onTouchListener 示例,我试图实现某种类似的,但我没能做到。

在我的 main.xml 中,我得到了一个包含 ViewSwitcher 和另外两个 LinearLayout 的线性布局。这些是我想要切换的布局。

这里是:

<ViewSwitcher
    android:id="@+id/viewSwitcher"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/firstlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/titlebar" />

        <Button
            android:id="@+id/somebut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30px"
            android:background="@drawable/but" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/secondlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second layout" >
        </TextView>
    </LinearLayout>
</ViewSwitcher>

和我的代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

public class SwitcherActivity extends Activity {
private float downXValue;
private View firstView;
private View SecondView;
private ViewSwitcher vs;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    vs =   (ViewSwitcher)findViewById(R.id.viewSwitcher);
    firstView= findViewById(R.id.firstlayout);
    secondView = findViewById(R.id.secondlayout);
    LinearLayout first = (LinearLayout) findViewById(R.id.firstlayout);
    first.setOnTouchListener((OnTouchListener) this);

}
public boolean onTouch(View arg0, MotionEvent arg1) {

    switch (arg1.getAction()){
        case MotionEvent.ACTION_DOWN:{
            // store the X value when the user's finger was pressed down
            downXValue = arg1.getX();
            break;
        }
        case MotionEvent.ACTION_UP:{
            // Get the X value when the user released his/her finger
            float currentX = arg1.getX();            
            // going backwards: pushing stuff to the right
            if (downXValue < currentX)
            {
                if (vs.getCurrentView() != myFirstView)vs.showPrevious();      
            }
            // going forwards: pushing stuff to the left
            if (downXValue > currentX)
            {
                if (vs.getCurrentView() != mySecondView)
                      vs.showNext();
            }
            break;
        }
    }
    return true;
}
}

我的问题是,我在包含以下内容的行中得到了 ClassCastException:

 first.setOnTouchListener((OnTouchListener) this);
4

1 回答 1

0

您的活动必须实现 OnTouchListener 以便“this”可以用作侦听器。现在它正试图将一个 Activity 投射到一个 OnTouchListener 上,这就是抛出 ClassCastException 的原因。

您还需要重写 OnTouchListener 方法来自定义处理触摸的方式。

于 2012-10-17T18:06:28.123 回答