layout - Android - Send touch event to other views -
i trying achieve scroll effect, think can done because see apps implemented this.
i have framelayout, in layout have: - recycler view - float view
<framelayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.recyclerview android:layout_width="match_parent" android:layout_height="match_parent"/> <linearlayout // float layout here android:layout_width="match_parent" android:layout_height="100dp"> </linearlayout> </framelayout> when scroll recycler view, can see float view scroll also, when reaches top of screen, want stop there. have implemented after face new issue. because float view above recycler view, can not scroll when touch , scroll float view. in case float view seems consumes touch event recycler nothing.
what want achieve when user want scroll recycler view should consume it. im thinking of sending float view's touch event recycler view.
thanks.
i have found same problem time ago. here solution (it little bit hacky, didn't find better solution). put in in custom framelayout class:
public class customframelayout extends framelayout { ... @injectview(r.id.rv_details) recyclerview recyclerview; @injectview(r.id.ll_details_action_bar_wrapper) viewgroup actionbarwrapperviewgroup; private list<motionevent> cachedeventlist = new arraylist<>(); private boolean touchisfromactionbar; private boolean ytranslationthresholdpassed; // pawel janeczek // 2 overrides forwarding touch events, started on action bar, recyclerview. // may ask, why there many lines? should recyclerview.dispatchtouchevent(ev) , should fine // because recyclerview when starting scrolling sends parent.requestdisallowintercepttouchevent disables sending onintercepttouchevent parent // in such case must set flag touchisfromactionbar when motion event starts , in action bar, , when flag set remove calling super on requestdisallowintercepttouchevent @override public boolean onintercepttouchevent(motionevent ev) { int action = ev.getaction(); if (action == motionevent.action_down && viewutils.iswithinviewbounds(actionbarwrapperviewgroup, ev.getrawx(), ev.getrawy())) { touchisfromactionbar = true; } if (touchisfromactionbar && shoulddispatcheventtorecyclerview(ev)) { if (!listutils.isempty(cachedeventlist)) { (motionevent motionevent : cachedeventlist) { recyclerview.dispatchtouchevent(motionevent); } cachedeventlist.clear(); } recyclerview.dispatchtouchevent(ev); } if (action == motionevent.action_cancel || action == motionevent.action_up) { cachedeventlist.clear(); ytranslationthresholdpassed = false; touchisfromactionbar = false; } return false; } private boolean shoulddispatcheventtorecyclerview(motionevent event) { if (ytranslationthresholdpassed) { return true; } else if (listutils.isempty(cachedeventlist)) { cachedeventlist.add(motionevent.obtain(event)); return false; } int ytranslationthreshold = 2; motionevent lastevent = listutils.getlast(cachedeventlist); if (math.abs(lastevent.gety() - event.gety()) > ytranslationthreshold) { ytranslationthresholdpassed = true; return true; } else { cachedeventlist.add(motionevent.obtain(event)); return false; } } @override public void requestdisallowintercepttouchevent(boolean disallowintercept) { if (!touchisfromactionbar) { super.requestdisallowintercepttouchevent(disallowintercept); } } ... } viewgroup named actionbarwrapperviewgroup flow layout in sample.
and xml customframelayout:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <recyclerview android:id="@+id/rv_details" android:layout_width="match_parent" android:layout_height="match_parent" /> <linearlayout android:id="@+id/ll_details_action_bar_wrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... <framelayout android:id="@+id/ll_details_action_bar_container" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="@dimen/default_bar_height" android:background="?colorprimary"/> ... </linearlayout> </merge> it live copied project, names can misleading think understandable. if have questions go on.
Comments
Post a Comment