layout - Android animation starts only after scrolling it's parent view -
i'm trying create expandable menu item in android, button , on button click, button expand down animation. set expand animation layout want expand when clicked view , have problem animation. doesn't start when clicked view, , starts when scroll-down or scroll-up container of view. , if container not scrollable, animation never starts. doing wrong?
here expand method, onclick method , layout xml file custom view things:
expand:
public void expand(final view v) { try { method m = v.getclass().getdeclaredmethod("onmeasure", int.class, int.class); m.setaccessible(true); m.invoke(v, measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec(((view)v.getparent()).getmeasuredwidth(), measurespec.at_most) ); } catch (exception e) { log.e(tag , "caught exception!", e); } final int initialheight = v.getmeasuredheight(); log.d("test", "initialheight="+initialheight); v.getlayoutparams().height = 0; v.setvisibility(view.visible); animation = new animation() { @override protected void applytransformation(float interpolatedtime, transformation t) { final int newheight = (int) (initialheight * interpolatedtime); v.getlayoutparams().height = newheight; v.requestlayout(); } @override public boolean willchangebounds() { return true; } }; a.setduration(1000); a.setinterpolator(animationutils.loadinterpolator(context, android.r.anim.accelerate_decelerate_interpolator)); v.startanimation(a); isexpanded = !isexpanded; } onclick:
public void onclick(view v) { if (!isexpanded) { expand(subbuttonslayout); } else { collapse(subbuttonslayout); } } layout xml custom menu item view:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata.trademaster" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal"> <linearlayout android:id="@+id/xexpandablemenubuttontop" android:background="@drawable/opened_menu_bg_top" android:layout_width="wrap_content" android:layout_height="wrap_content"> </linearlayout> <linearlayout android:background="@drawable/opened_menu_bg_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_vertical"> <linearlayout android:id="@+id/xexpandablemenubuttontitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <textview android:id="@+id/xexpandablemenubuttontext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginleft="10dip" android:layout_marginright="10dip" android:textappearance="@style/expandable_menu_button_textstyle" android:text="button text"> </textview> <imageview android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="6" android:src="@drawable/menu_button_down_arrow"> </imageview> </linearlayout> </linearlayout> <linearlayout android:id="@+id/xexpandablemenubuttonsubbuttonslayout" android:background="@drawable/opened_menu_bg_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_vertical" android:visibility="gone"> <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <com.myproject.control.xsubmenubutton android:layout_width="wrap_content" android:layout_height="wrap_content" mtx:xsubmenubuttontext="submenu1"> </ccom.myproject.control.xsubmenubutton> <com.myproject.control.xsubmenubutton android:layout_width="wrap_content" android:layout_height="wrap_content" mtx:xsubmenubuttontext="submenu2"> </com.myproject.control.xsubmenubutton> <com.myproject.control.xsubmenubutton android:layout_width="wrap_content" android:layout_height="wrap_content" mtx:xsubmenubuttontext="submenu3"> </com.myproject.control.xsubmenubutton> </linearlayout> </linearlayout> <linearlayout android:id="@+id/xexpandablemenubuttonbottom" android:background="@drawable/opened_menu_bg_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content"> </linearlayout> </linearlayout>
if haven't solved problem or if else comes across problem, here quick solution. invalidate parent view on each click (in onclick method). should work regardless of if parent scrollable or not.
that is, code like:
public void onclick(view v) { if (!isexpanded) { expand(subbuttonslayout); } else { collapse(subbuttonslayout); } rootview.invalidate(); }
Comments
Post a Comment