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

时间:2011-08-25 23:56:13

标签: android swipe swipe-gesture

我是Android新手,我正在尝试创建一个页面,我可以从左向右和从右向左滑动以转到上一页和下一页。我花了很多时间抬头尝试不同的东西。出于某种原因,当我滑动(无论哪个方向)时,它只显示下一张图片。当我从右向左滑动时,它应该显示我之前的图片。任何帮助将非常感谢!非常感谢您的时间:)

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
    }

 }
}

提前致谢!!

2 个答案:

答案 0 :(得分:1)

注释掉

iv.setOnClickListener

在你的活动中,我认为这应该是有效的。

答案 1 :(得分:0)

对不起,我不是超级知识,我实际上是在尝试自己处理这个问题...我正在使用钛api进行编程所以我不熟悉你的代码的语法,虽然我得到了一般的想法。 ..(我的实习期限为2周)

到了这一点,我很早就看到了以下几行代码

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

看起来像你的等式

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

可以检测滑动...但是代码中的ABS()功能会导致您仅解释滑动的幅度,并且无法区分方向。希望这有帮助!