X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FRecyclerItemListener.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FRecyclerItemListener.java;h=64427c2e2cf3b1d2d9c1dc6a5d529fed488d9e58;hp=0000000000000000000000000000000000000000;hb=0bbdc409d82da31324c031f36607510f17d992e6;hpb=8c51ed6deeca6a66d6120f3d967c3ceafb1eb226 diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/RecyclerItemListener.java b/app/src/main/java/net/ktnx/mobileledger/ui/RecyclerItemListener.java new file mode 100644 index 00000000..64427c2e --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/RecyclerItemListener.java @@ -0,0 +1,73 @@ +/* + * Copyright © 2018 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.ui; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.RecyclerView.OnItemTouchListener; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.view.View; + +public class RecyclerItemListener implements OnItemTouchListener { + private RecyclerTouchListener listener; + private GestureDetector gd; + + public interface RecyclerTouchListener { + void onClickItem(View v, int position); + void onLongClickItem(View v, int position); + } + + public RecyclerItemListener(Context ctx, RecyclerView rv, RecyclerTouchListener listener) { + this.listener = listener; + this.gd = new GestureDetector( + ctx, new GestureDetector.SimpleOnGestureListener() { + @Override + public void onLongPress(MotionEvent e) { + View v = rv.findChildViewUnder(e.getX(), e.getY()); + listener.onLongClickItem(v, rv.getChildAdapterPosition(v)); + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + View v = rv.findChildViewUnder(e.getX(), e.getY()); + listener.onClickItem(v, rv.getChildAdapterPosition(v)); + return true; + } + } + ); + } + + @Override + public boolean onInterceptTouchEvent(@NonNull RecyclerView recyclerView, + @NonNull MotionEvent motionEvent) { + View v = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); + return (v != null) && gd.onTouchEvent(motionEvent); + } + + @Override + public void onTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent) { + + } + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean b) { + + } +}