How to hold and drag (re-position) a layout along with its associated layouts in android -
i making android app. in have scenario. first watch screen shot below 1 come here answer has clear picture want.
scenario: activity having map below navigation bar, relativelayout (red background) in center, listview below red relativelayout
what want: want drag or re-position (whatever term may used others) red relativelayout holding finger on , move , down on screen along listview below should move layout
code tried:
xml layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <relativelayout android:layout_width="match_parent" android:layout_height="@dimen/fifty_five_dp" android:id="@+id/topbar" android:background="@color/bg_color" > <button android:id="@+id/button_menu" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="4dp" android:background="@drawable/list_btn" android:onclick="togglemenu" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="watchover" android:textcolor="@android:color/white" android:textsize="@dimen/eighteen_sp" android:textstyle="bold" /> </relativelayout> <!-- fragment show --> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/topbar" android:id="@+id/root" android:background="@android:color/white" > <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/greenbarimage" /> <relativelayout android:id="@+id/redlayout" android:layout_width="match_parent" android:layout_height="@dimen/forty_dp" android:layout_centervertical="true" android:background="@color/btn_color" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="fences info" android:textcolor="@android:color/white" android:textsize="@dimen/sixteen_sp" /> <button android:id="@+id/btn_drag" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="4dp" android:background="@drawable/list_btn" android:onclick="togglemenu" android:visibility="gone" /> </relativelayout> <listview android:id="@+id/list_devices" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/greenbarimage" android:padding="@dimen/five_dp" android:scrollbars="none" /> </relativelayout> </framelayout> </relativelayout>
java code:
public class myactivity extends fragmentactivity implements ontouchlistener { private int _xdelta; private int _ydelta; relativelayout redlayout; framelayout rootlayout; listview deviceslist; @override protected void oncreate(bundle arg0) { super.oncreate(arg0); setcontentview(r.layout.main_screen); deviceslist = (listview) findviewbyid(r.id.list_devices); rootlayout = (framelayout) findviewbyid(r.id.root); redlayout = (relativelayout) findviewbyid(r.id.redlayout); redlayout.setontouchlistener(this); } @override public boolean ontouch(view view, motionevent event) { final int x = (int) event.getrawx(); final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: relativelayout.layoutparams lparams = (relativelayout.layoutparams) view.getlayoutparams(); _xdelta = x - lparams.leftmargin; _ydelta = y - lparams.topmargin; break; case motionevent.action_up: break; case motionevent.action_pointer_down: break; case motionevent.action_pointer_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view.getlayoutparams(); layoutparams.leftmargin = x - _xdelta; layoutparams.topmargin = y - _ydelta; layoutparams.rightmargin = -250; layoutparams.bottommargin = -250; view.setlayoutparams(layoutparams); break; } rootlayout.invalidate(); return true; } }
source getting above code here
i tried code not getting desired result. when drag red relativelayout , down, listview overlay on layout , not adjusted want. type of appreciated.
material way
you can archive it(or, @ least, close want)
coordinatorlayout
, keeping map layout insidecollapsingtoolbarlayout
.your layout can this:
<android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.collapsingtoolbarlayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollflags="scroll|exituntilcollapsed"> <!-- map --> <relativelayout ... app:layout_collapsemode="parallax"> ..... </relativelayout> <!-- toolbar --> <android.support.v7.widget.toolbar android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:layout_collapsemode="pin" /> </android.support.design.widget.collapsingtoolbarlayout> <!-- red thingy, if need --> <view android:layout_width="match_parent" android:layout_height=".."/> </android.support.design.widget.appbarlayout> <!--list of content items --> <android.support.v7.widget.recyclerview android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
you can read more here android design support library , here design support library (iii): coordinator layout
view.ontouchlistener
way(this 1 use)
idea simple:
- set map
android:layout_above
red separator & listview -android:layout_below
red separator - once moving action done - translate separator respectively or down , set proper top&bottom mangins
here's how looks:
public class mainactivity extends appcompatactivity implements view.ontouchlistener { private int _ydelta; @override protected void oncreate(bundle arg0) { super.oncreate(arg0); setcontentview(r.layout.activity_main); findviewbyid(r.id.separator).setontouchlistener(this); } @override public boolean ontouch(view view, motionevent event) { final int y = (int) event.getrawy(); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: relativelayout.layoutparams lparams = (relativelayout.layoutparams) view.getlayoutparams(); _ydelta = y - lparams.bottommargin; break; case motionevent.action_up: break; case motionevent.action_pointer_down: break; case motionevent.action_pointer_up: break; case motionevent.action_move: relativelayout.layoutparams layoutparams = (relativelayout.layoutparams) view.getlayoutparams(); layoutparams.bottommargin = (y - _ydelta); layoutparams.topmargin = -layoutparams.bottommargin; view.setlayoutparams(layoutparams); view.animate().translationy(y - _ydelta).setduration(0); break; } findviewbyid(r.id.root).invalidate(); return true; } }
and
activity_main.xml
:<relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root"> <view android:background="#aa0f00ff" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/separator"/> <view android:layout_below="@+id/separator" android:background="#aa0ff000" android:layout_width="match_parent" android:layout_height="match_parent" /> <view android:background="#ff0000" android:layout_centervertical="true" android:id="@+id/separator" android:layout_width="match_parent" android:layout_height="20dp" /> </relativelayout>
- set map
i hope, helps!
Comments
Post a Comment