2015-10-05 135 views
0

我的目的是在佈局中實施滑動。在佈局中從左側或右側滑動時,可以看到並隱藏一個視圖。如何在Android Studio的佈局中實施滑動(左/右)手勢?

正在使用Android Studio。有沒有可以直接使用的庫?

+1

您可以看看ViewPager。請參閱[Google的文檔](https://developer.android.com/training/animation/screen-slide.html)。 –

+0

我只需要在頁面中的小布局內滑動。就像你在框中有兩個圖像,從左到右滑動一個將顯示,其他將是部分,反之亦然。我無法在此處上傳屏幕截圖。 – dreamdeveloper

+0

我認爲你正在嘗試實現一個像tinder.right的佈局? –

回答

1

您可以使用onfling手勢監聽器。下面給出您的示例代碼

public class MyActivity extends Activity { 
private void onCreate() { 
// Set your layout 
final ImageView imageView = (ImageView) findViewById(R.id.image_view); 
imageView.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(final View view, final MotionEvent event) { 
     return gdt.onTouchEvent(event); 
    } 
}); 
    } 

    private final GestureDetector gdt = new GestureDetector(new  GestureListener()); 

    private class GestureListener extends SimpleOnGestureListener { 

private final int SWIPE_MIN_DISTANCE = 120; 
private final int SWIPE_THRESHOLD_VELOCITY = 200; 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >  SWIPE_THRESHOLD_VELOCITY) { 
     // Right to left, your code here 
     return true; 
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &&  Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     // Left to right, your code here 
     return true; 
    } 
    if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) >  SWIPE_THRESHOLD_VELOCITY) { 
     // Bottom to top, your code here 
     return true; 
    } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
     // Top to bottom, your code here 
     return true; 
    } 
    return false; 
    } 
    } 
    }