安卓:刷卡从左到右,从右到左 [英] Android: Swipe left to right and right to left

查看:163
本文介绍了安卓:刷卡从左到右,从右到左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,我试图做一个网页,我可以刷卡由左到右,从右到左去previous和下一页。我花了很多时间寻找和尝试不同的东西。出于某种原因,当我刷(无论哪个方向),那只能说明下一张照片。它应该显示我previous图片时,我刷卡从右到左。任何帮助将是非常美联社preciated!非常感谢您的时间:)

I am new to Android, and I am trying to make a page where I can swipe from left to right and right to left to go to previous and next pages. I spent a lot of time looking up and trying different things. For some reason, when I swipe (no matter which direction), it only shows next picture. It should have shown me previous picture when I swipe right to left. Any help will be highly appreciated! Thanks so much for your time :)

public class MyGestureDetector extends SimpleOnGestureListener
{
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 isRightToLeft = false;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
{
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
        // right to left swipe
     // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            isRightToLeft = true;
            return true;
        }               
       // left to right swipe
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            isRightToLeft = false;
            return true;

        }
    } catch (Exception e) {
        // nothing
    }
    return false;
}

}






public class Pictures extends Activity 
{
private int pictureCounter = 1;
private Context myContext = this;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

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


    final MyGestureDetector myGestureDetector = new MyGestureDetector(); 
    // Gesture detection
    gestureDetector = new GestureDetector(myGestureDetector);
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                if (myGestureDetector.isRightToLeft)
                {
                    previousPicture();
                }
                else if (!myGestureDetector.isRightToLeft)
                {
                    nextPicture();
                }
                return true;
            }
            return false;
        }
    };
    //iv.setOnTouchListener(gestureListener);
    ((ImageView)findViewById(R.id.title_pictures)).setOnTouchListener(gestureListener);


    ImageView iv = (ImageView) findViewById(R.id.flag_pic);
    iv.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            nextPicture();
        }
    });
}

public void getPicture(int picCounter)
{
    DatabaseHelper myDbHelper = new DatabaseHelper(this);

    try
    {
        myDbHelper.createDatabase();
    }
    catch (IOException ioe)
    {
        throw new Error("Unable to create databse");
    }

    try
    {
        myDbHelper.openDatabase();
    }
    catch(SQLException sqle)
    {
        throw sqle;
    }

    String query = "select Before_Picture, After_Picture from picture_mapping where _id = " + picCounter;

    SQLiteDatabase db = myDbHelper.getReadableDatabase();

    Cursor cursor = db.rawQuery(query, null);
    cursor.moveToFirst();
    String beforePicture = cursor.getString(0);
    String afterPicture = cursor.getString(1);

    cursor.close();
    db.close();

    ImageView before_pic = (ImageView) findViewById(R.id.imgView_before_pic);
    int resId = myContext.getResources().getIdentifier(beforePicture,"drawable", "com.ash.android.pictures");
    before_pic.setBackgroundResource(resId);

    TextView after_pic = (TextView) findViewById(R.id.txtView_after_picture);
    after_pic.setText(afterPicture);
    //Toast toast=Toast.makeText(this, beforePicture+ ":" + afterPicture, Toast.LENGTH_LONG);  
    //toast.show();  



}

public void nextPicture()
{
    if (pictureCounter < 36)
    {
        pictureCounter += 1;
        getPicture(pictureCounter);
    }
    else
    {
        //do nothing
    }
}

public void previousPicture()
{
    if (pictureCounter > 1)
    {
        pictureCounter -= 1;
        getPicture(pictureCounter);
    }
    else
    {
        //do nothing
    }

 }
}

在此先感谢!

Thanks in advance!!

推荐答案

注释掉

iv.setOnClickListener

在您的活动,我认为一扔应工作。

in your activity and I think the fling should be working.

这篇关于安卓:刷卡从左到右,从右到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆