手势和OnClick崩溃了我的应用程序

时间:2013-09-27 10:27:09

标签: android swipe gesture onclicklistener

我有3个视图和一些图像,我已经在视图中添加了ViewFlipper,因此我可以使用手势从一个视图滑动到另一个视图。问题是,当我点击图像打开其他活动时,我希望能够。但使用onClick只会破坏我的应用程序。

我搜索了论坛以找到答案,但我无法解决我的问题。

这是我的代码:

package gr.happyimgs;

import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Learn extends Activity implements OnClickListener{
private ViewFlipper page;
private Animation animFlipInForeward;
private Animation animFlipOutForeward;
private Animation animFlipInBackward;
private Animation animFlipOutBackward;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.learn_layout);

    page = (ViewFlipper) findViewById(R.id.flipper);

    ImageView img1 = (ImageView)findViewById(R.id.img1);
    ImageView img2 = (ImageView)findViewById(R.id.img2);

    img1.setOnClickListener(this);
    img2.setOnClickListener(this);

    animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin);
    animFlipOutForeward = AnimationUtils
            .loadAnimation(this, R.anim.flipout);
    animFlipInBackward = AnimationUtils.loadAnimation(this,
            R.anim.flipin_reverse);
    animFlipOutBackward = AnimationUtils.loadAnimation(this,
            R.anim.flipout_reverse);

    setupActionBar();
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    onBackPressed();
    return true;
}

public void setupActionBar() {
    String loc = Locale.getDefault().getLanguage();

    ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(R.layout.learn_title);

    Typeface font = Typeface.createFromAsset(getAssets(), "SNAP.TTF");
    Typeface font_el = Typeface.createFromAsset(getAssets(), "COMIC.TTF");

    TextView title = (TextView) actionBar.getCustomView().findViewById(
            R.id.learn_title_text);

    if (loc.equals("el")) {
        title.setTypeface(font_el);
        title.setTextSize(20);
    } else {
        title.setTypeface(font);
        title.setTextSize(20);
    }
}

private void SwipeRight() {
    page.setInAnimation(animFlipInBackward);
    page.setOutAnimation(animFlipOutBackward);
    page.showPrevious();
}

private void SwipeLeft() {
    page.setInAnimation(animFlipInForeward);
    page.setOutAnimation(animFlipOutForeward);
    page.showNext();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(event);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

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

        float sensitvity = 50;
        if ((e1.getX() - e2.getX()) > sensitvity) {
            SwipeLeft();
        } else if ((e2.getX() - e1.getX()) > sensitvity) {
            SwipeRight();
        }

        return true;
    }

};

GestureDetector gestureDetector = new GestureDetector(getBaseContext(),
        simpleOnGestureListener);

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.img1:
        Intent img1Intent = new Intent(this, First.class);
        startActivity(img1Intent);
        break;
    case R.id.img2:
        Intent img2Intent = new Intent(this, Second.class);
        startActivity(img2Intent);
        break;
        default:
            break;
    }
}
}

0 个答案:

没有答案