Reputation: 367
I have a View
defined in an XML, what I want is to let the user slide the view up and return if it doesn't go up half of the bar. But I don't find any links that help on Google search after some time of Googling. Any ideas would be greatly appreciated, thanks!
Upvotes: 0
Views: 1334
Reputation: 179
For moving a view with finger you can use the following idea: detect touches when user moves his finger and move the view according to the shift.
GestureDetector gestureDetector = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent start, MotionEvent event, float distanceX, float distanceY) {
view.setTranslationY(event.getY()-start.getY());
return true;
}
});
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
gestureDetector.onTouchEvent(event); // here we pass events to detector above
return false;
}
});
If you want to return the view to initial position when user stops sliding, in onTouch
(see above) write this:
if(event.getActionMasked()==MotionEvent.ACTION_UP) {
view.setTranslationY(0);
}
Upvotes: 2