English 中文(简体)
安德列斯:Swipe左上右边和左边右边。
原标题: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. 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
    }

 }
}

预告!

问题回答

评论

iv.setOnClickListener

在你的活动中,我认为,这一ling子应该发挥作用。

令我感到悲伤的是,我不知道并实际上试图自己处理这个问题。 我正在使用 t皮进行编程,因此,我不熟悉你的法典的同义,尽管我有一般的想法。 (我的实习培训期为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)

将努力发现一种pe。 但是,你的代码中的)功能将只使你解释pe的大小,并且无法说明方向上的区别。 希望这一帮助!





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签