问题
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));
回答1:
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.
回答2:
Not really a solution but the problem is with the HorizontalListView that I found because it works for a normal listview
来源:https://stackoverflow.com/questions/17747123/layoutparams-not-changing