我有一个带孩子的RecyclerView。当我触摸孩子时,显示DialogFragment
,当我发布时,DialogFragment
被解雇。但是onTouch
仅在子区域内触发。我想让onTouch
ACTION_UP
触发整个屏幕。
RecyclerView
自定义OnItemTouchListener
:
galleryRecycler.addOnItemTouchListener(
new ClosetGalleryAdapter.RecyclerTouchListener(
getContext().getApplicationContext(),
galleryRecycler,
new ClosetGalleryAdapter.ClickListener() {
ClosetFragDialog dialogFragment;
FragmentManager fm;
boolean is_pressed = false;
boolean isLongCLick = false;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
if (is_pressed) {
dialogFragment.show(fm, "info_frag");
isLongCLick = true;
}
}
};
@Override
public void onTouch(View view, int position, MotionEvent e) {
Bundle bundle = new Bundle();
bundle.putSerializable("look", looks.get(position));
fm = getFragmentManager();
dialogFragment = new ClosetFragDialog();
dialogFragment.setArguments(bundle);
if (e.getAction() == MotionEvent.ACTION_DOWN) {
is_pressed = true;
// 500ms - to determine that this is a long press
handler.postDelayed(runnable, 300);
} else if (e.getAction() == MotionEvent.ACTION_UP && is_pressed && isLongCLick) {
DialogFragment frag = (DialogFragment) fm.findFragmentByTag("info_frag");
frag.dismiss();
is_pressed = false;
isLongCLick = false;
} else if (e.getAction() == MotionEvent.ACTION_UP && is_pressed) {
Toast.makeText(getContext(), "Click!", Toast.LENGTH_SHORT).show();
is_pressed = false;
isLongCLick = false;
} else {
}
}
}));
自定义OnItemTouchListener
:
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private ClosetGalleryAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child != null && clickListener != null) {
clickListener.onTouch(child, rv.getChildAdapterPosition(child), e);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
接口:
public interface ClickListener {
void onTouch(View view, int position, MotionEvent e);
}