简体   繁体   English

Android:从左向右滑动,从右向左滑动

[英]Android: Swipe left to right and right to left

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. 我是Android的新手,我正在尝试创建一个页面,在其中我可以从左向右滑动,从右向左滑动,以转到上一页和下一页。 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!! 提前致谢!!

Comment out the 注释掉

iv.setOnClickListener

in your activity and I think the fling should be working. 在您的活动中,我认为应该有效。

Sorry I am not super knowledgable and am actually trying to work on this question myself... I am using titanium api in order to program so I am not familiar with the syntax of your code, though I get the general idea... (Still on a 2 week training period for my internship) 抱歉,我不是超级知识渊博的人,实际上我自己正在尝试解决此问题...我使用Titan api进行编程,因此尽管我有一般的想法,但我并不熟悉您代码的语法...(仍处于为期2周的实习期间)

TO THE POINT THOUGH, I see the following lines of code pretty early on 要点,我很早就看到了以下几行代码

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

It looks like your equation 看起来像你的方程式

if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX)

will work to detect a swipe... However the ABS() function in your code will cause you to only interpret the magnitude of the swipe, and will be unable to tell a difference in direction. 将可检测到滑动...但是,代码中的ABS()函数将使您仅解释滑动的幅度,并且无法分辨方向。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM