在Fling手势事件不在Tabactivity内工作

时间:2011-12-05 00:38:03

标签: android

我有Tabactivity的问题。 我有一个ReweHaupt课程。在ReweHaupt类中,我有一个viewflipper。但是Gestureevent不在这堂课上工作。我现在该怎么办

ReweHaupt.Java

public class ReweHaupt extends Activity implements OnClickListener{
   private ViewFlipper vf;
   private Button btnNext,btnPrevious;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rewehaupt);

    vf = (ViewFlipper) findViewById(R.id.viewFlipper1);
    btnNext = (Button) findViewById(R.id.btnNext);
    btnPrevious = (Button) findViewById(R.id.btnPrevious);

    btnNext.setOnClickListener(this);
    btnPrevious.setOnClickListener(this);



}

 public void onClick(View v) {
switch(v.getId())
{
case R.id.btnNext:
  //Button Next Style
  Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
  btnNext.startAnimation(animationNext);

  nextView();
  break;

case R.id.btnPrevious:
  //Button Previous Style
  Animation animationPrevious = AnimationUtils.loadAnimation(this,R.anim.btn_style_previous);
  btnPrevious.startAnimation(animationPrevious);

   previousView();
   break;
}}

   private void previousView() {


  vf.setInAnimation(this, R.anim.in_animation1);
  vf.setOutAnimation(this, R.anim.out_animation1);
  vf.showPrevious();}

 private void nextView() {

  vf.setInAnimation(this, R.anim.in_animation);
  vf.setOutAnimation(this, R.anim.out_animation);
  vf.showNext();}}

rewe2.java

public class rewe2 extends TabActivity implements OnGestureListener {

private GestureDetector gestureDetector = null;
private ViewFlipper vf2;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rewe2);


    gestureDetector = new GestureDetector(this, this);


    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;
    Resources res = getResources();

    intent = new Intent().setClass(this, ReweHaupt.class);
    spec = tabHost.newTabSpec("ReweHaupt").setIndicator("ReweHaupt",
      res.getDrawable(R.drawable.home))

                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MeinEinKaufListe.class);
    spec = tabHost.newTabSpec("EinkaufListe").setIndicator("EinkaufListe",res.getDrawable(R.drawable.einkaufliste))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MeinRewe.class);
    spec = tabHost.newTabSpec("Filiarsuchen").setIndicator("Filiarsuchen",res.getDrawable(R.drawable.searchbuton))
                  .setContent(intent);
    tabHost.addTab(spec);





}

     public boolean onTouchEvent(MotionEvent event) {
  if (gestureDetector.onTouchEvent(event))
  return true;
  else
  return false;
  }


    public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;}

    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;

    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
  vf2.showPrevious();
}

// 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
  vf2.showNext();
}

return false; }


public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
    float distanceY) {
// TODO Auto-generated method stub
return false; }

public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub }

public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false} }

rewe2.xml

<?xml version="1.0" encoding="utf-8"?>
  <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/linearLayout1"
    android:layout_height="match_parent">




    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

    </TabWidget>


    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@android:id/tabs" >

    </FrameLayout>
</RelativeLayout>

rewehaupt.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="@drawable/reweback">

<AbsoluteLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="60dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="59dp"
        android:layout_y="20dp"
        android:text="Angebot Der Woche"
        android:textColor="#000000"
        android:textSize="25dp" />

</AbsoluteLayout>

<AbsoluteLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="239dp"
    android:layout_weight="0.08"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="176dp"
        android:layout_y="235dp"
        android:background="@drawable/next" />

    <Button
        android:id="@+id/btnPrevious"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="96dp"
        android:layout_y="236dp"
        android:background="@drawable/previous" />

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="fill_parent"
        android:layout_height="224dp"
        android:layout_x="0dp"
        android:layout_y="0dp" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/cola" />

          <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/elma" />

            <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/muz" />

              <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/pepsi" />

                <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/whopper" />



    </ViewFlipper>

</AbsoluteLayout>

0 个答案:

没有答案