animation - Android Expand/Collapse Gridview -
i want expand , colapse gridview. when expand went good, collapse, too, problem when go expand, 1 row displayed.
my gridview:
public class mygridview extends gridview { public mygridview(context context) { super(context); } public mygridview(context context, attributeset attrs) { super(context, attrs); } public mygridview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override public void setvisibility(int visibility) { // todo auto-generated method stub super.setvisibility(visibility); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int heightspec; if (getlayoutparams().height == layoutparams.wrap_content) { heightspec = measurespec.makemeasurespec( integer.max_value >>2, measurespec.at_most); } else { // other height should respected is. heightspec = heightmeasurespec; } super.onmeasure(widthmeasurespec, heightspec); } }
expand/collapse:
private void expand() { //set visible gridview.setvisibility(view.visible); final int widthspec = view.measurespec.makemeasurespec(0, view.measurespec.unspecified); final int heightspec = view.measurespec.makemeasurespec(0, view.measurespec.unspecified); gridview.measure(widthspec, heightspec); valueanimator manimator = slideanimator(0, gridview.getmeasuredheight()); manimator.start(); } private void collapse() { int finalheight = gridview.getheight(); valueanimator manimator = slideanimator(finalheight, 0); manimator.addlistener(new animator.animatorlistener() { @override public void onanimationend(animator animator) { //height=0, set visibility gone gridview.setvisibility(view.gone); } ... }); manimator.start(); } private valueanimator slideanimator(int start, int end) { valueanimator animator = valueanimator.ofint(start, end); animator.addupdatelistener(new valueanimator.animatorupdatelistener() { @override public void onanimationupdate(valueanimator valueanimator) { //update height int value = (integer) valueanimator.getanimatedvalue(); viewgroup.layoutparams layoutparams = gridview.getlayoutparams(); layoutparams.height = value; gridview.setlayoutparams(layoutparams); } }); homecoming animator; }
what doing wrong?
i solved problem adding globallayoutlistener gridview, store height , on expand replace:
valueanimator manimator = slideanimator(0, gridview.getmeasuredheight());
with:
valueanimator manimator = slideanimator(0, stored_height);
android animation gridview
No comments:
Post a Comment