2

I am trying to animate a horizontal list item dismiss the alpha animation works and the layoutparam values also decrease over time but for some reason that doesn't change the actual height of the list item.

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    final int initialHeight = view.getMeasuredHeight();


    if(interpolatedTime == 1){

        imageAdapter.remove(view, position);


    }else{

        view.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
        view.setAlpha(1 - interpolatedTime);
        System.out.println("  v height = " + view.getHeight());
        System.out.println("  v layoutparams = " + view.getLayoutParams().height);
        view.forceLayout();
        view.invalidate();
        view.requestLayout();

    }
}

this is in the getView method of the imadeAdapter and binBtn is the button which you touch to remove the listitem

      binBtn.setOnTouchListener(new OnTouchListener() {


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub


                  if (event.getAction() == MotionEvent.ACTION_UP)
                      {

                      View vp = (View) v.getParent();

                SqueezeAnimation ani = new SqueezeAnimation(this,vp,position);
                ani.setDuration(1000);
                v.startAnimation(ani);

change to this code after Tanis' answer but still same problem, the getHeight() prints 674 and getLayoutParams().height prints 100

         else{
         RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(100, 100);

        view.setLayoutParams(lps);
        view.requestLayout();

        System.out.println("  v height = " + view.getHeight());
        System.out.println("  v layoutparams = " + view.getLayoutParams().height);

as a side note this is in the getView method of the imageadapter and that changes the height of that view, and ive tried passing in the backgroundfillIV to animate but still doesnt work

  View backgroundfillIV =  gridView.findViewById(R.id.backroundfillbar);
       double mult =  products.get(position).value/ products.get(0).value;
       backgroundfillIV.getLayoutParams().height = (int) ( (((bottleheight+20)*0.95)/mult)); 
Thomas
  • 435
  • 6
  • 20

2 Answers2

7

You need to call setLayoutParams() at some point to save your changes to the LayoutParams.

For example:

LayoutParams lps = view.getLayoutParams();
lps.height = initialHeight - (int)(initialHeight * interpolatedTime);
view.setAlpha(1 - interpolatedTime);
view.setLayoutParams(lps);

This is because the getLayoutParams() method is only returning a copy of the LayoutParams reference, not a reference to the LayoutParams themselves. There is a bit more in-depth discussion on this at Does Java return by reference or value.

Community
  • 1
  • 1
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • thanks for the answer, i really thought that this would solve it but it still doesn't change the height. – Thomas Jul 19 '13 at 14:27
  • Instead of getting the existing LayoutParams, try creating an entirely new set of LayouParams. – Bryan Herbst Jul 19 '13 at 14:30
  • i used `LayoutParams lps = new LayoutParams(100, 100);` and that changes the layoutparams to 100 but still doesnt change the actual dimensions. I imported `import android.view.ViewGroup.LayoutParams;` is that the right one, there where a lot – Thomas Jul 19 '13 at 14:35
  • You should use the LayoutParams from the View's parent. If this View is in a LinearLayout, use LinearLayout.LayoutParams; if the View is in a RelativeLayout, use RelativeLayout.LayoutParams; etc. – Bryan Herbst Jul 19 '13 at 14:48
  • I'm afraid I'm not sure what's wrong then. Make sure that you update your `backgroundfillIV` LayoutParam code similarly. Do some more debugging and experimenting (perhaps create a new app to play with) to figure out where it is going wrong. – Bryan Herbst Jul 19 '13 at 15:09
  • view.requestLayout() may help, depending on the params changed may need to call requestLayout on parent view. – izzy Apr 14 '16 at 06:36
  • Perfect !! You save my day – karanatwal.github.io Mar 07 '17 at 10:41
0

Not really a solution but the problem is with the HorizontalListView that I found because it works for a normal listview

Thomas
  • 435
  • 6
  • 20