I have trouble implementing something like Instagram preview image feature (on long press show dialog with enlarged image). So i have Fragment
with ViewPager
as its child. Adapter
responsible for feeding the data to theViewPager
has one imageView with onTouchListener
. That listener has gestureDetector
for detecting onDoubleTap
and onLongPress
. Now the thing is that i want onLongPress
to inflate the Dialog
in the parent Fragment
, and on release i want to dismiss the Dialog
. But for some reason, MotionEvent.ACTION_UP
or MotionEvent.ACTION_CANCEL
never gets called in the pagerAdapter, i guess that Dialog is intercepting the actions, and it is so frustrating, i cant implement this for days... Simply cant figure it how does TouchEvent
system works.. Here is the code:
PagerAdapter:
private class MyOnTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e)
listener.performSomeAction();
return super.onDoubleTap(e);
}
@Override
public void onLongPress(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
listener.setZoomLayout(imageUri);
break;
case (MotionEvent.ACTION_UP):
listener.hideZoomLayout();
break;
case MotionEvent.ACTION_CANCEL:
listener.hideZoomLayout();
break;
}
super.onLongPress(e);
}
});
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (view.getId()) {
case R.id.ivSelectedImg1:
gestureDetector.onTouchEvent(event);
return true;
case R.id.ivSelectedImg2:
gestureDetector.onTouchEvent(event);
return true;
case R.id.ivSelectedImg3:
gestureDetector.onTouchEvent(event);
return true;
case R.id.ivSelectedImg4:
gestureDetector.onTouchEvent(event);
return true;
default: return false;
}
}
}
Fragment:
@Override
public void setZoomLayout(String imageUri) {
View view = getLayoutInflater().inflate(R.layout.layout_image_zoom, null);
ImageView zoomImage = view.findViewById(R.id.ivImageZoom);
zoomImage.setClickable(false);
zoomImage.setFocusable(false);
GlideApp.with(getActivity())
.load(imageUri)
.into(zoomImage);
dialogBuilder = new Dialog(getActivity(), -1);
dialogBuilder.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogBuilder.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
dialogBuilder.setContentView(view);
dialogBuilder.show();
Don`t get how to prevent inflating Dialog to take over the MotionEvent. I mean, i am not even sure that event is getting caught in the dialog, looks like its gone. Thanks a lot in advance!