Android View with a translate animation - positioning is not working as expected
<p>I have a View that has an <code>OnClickListener</code>. When clicked, the view translates up to a certain position on the page. This is no problem, the view goes where it should. When the view is clicked again, I would like to position it somewhere else, but this is not the case. After a little bit of trouble shooting, I found that my View's getTop() method returns the same value - even after the translation animation has moved the view to a different part of the screen. For the second animation, it is not using the current position (as I would expect), it instead uses the initial position.</p>

<p>Few things that I am doing: I am using the <code>ObjectAnimation</code> class rather than the <code>TranslationAnimation</code> class, since I wanted to keep the <code>OnClickListener</code> functioning. With the <code>TranslationAnimation</code> class, I found that the view was correctly moved, but the <code>OnClickListener</code> was only working in the area that the View started from. Using the <code>ObjectAnimation</code> class, I was able to easily get the translation to work AND the <code>OnClickListener</code> functions correctly - it is triggered where the view currently is on the screen.</p>

<p>Here's what I have so far:</p>

<pre><code>final LinearLayout child = layouts.get(i); //ArrayList containing some dynamic layouts
final int offset = target - child.getTop(); 
ObjectAnimator anim = ObjectAnimator.ofFloat(child,"translationY",offset);
anim.setDuration(250);
anim.start();
</code></pre>

<p>This is what happens when the view is clicked the first time. It translates up along the Y axis, where the offset determines how far the View needs to move from its current position.</p>

<p>Now, here's what happens on the second click. The goal here was to align the view with the parent's base.</p>

<pre><code>target = parent.getBottom();
offset = target - child.getTop();
anim = ObjectAnimator.ofFloat(child, "translationY",offset);
anim.setDuration(250);
anim.start();
prev = child;
</code></pre>

<p>This is where things fall apart - <code>child.getTop()</code> returns the Y coordinate of the view's ORIGINAL position. Not the current position. So after the animation, the view is placed well below the bottom of the parent. I read a different question which stated that I should use <code>child.getY()</code> instead, which is supposed to give me the <code>translationY</code> position plus the <code>top</code> position, but this didn't lead to any better results. I can't seem to get this to work just right. I'd simply like to move the view from its current position to the bottom of the screen, but this appears to be a hard thing to accomplish. Any help would be appreciated. </p>

<p><strong>EDIT</strong></p>

<p>I have added an animation listener:</p>

<pre><code>ObjectAnimator anim = ObjectAnimator.ofFloat(child,"translationY",offset);
anim.setDuration(250);
anim.addListener(new ObjectAnimator.AnimatorListener(){
    @Override
    public void onAnimationStart(Animator animation) {
        System.out.println("start: " + child.getTop() + " " + child.getY());
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        System.out.println("end: " + child.getTop() + " " + child.getY() + " " + child.getTranslationY());
        child.setTop((int)child.getY());
        System.out.println(child.getTop());
    }

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {}
});
anim.start();
</code></pre>

<p>Here I am setting the listener to try to change where the Top of the view is located. Behaviour is again not working as expected. The view is actually sent up above the screen when I do this. Output of the System.out looks like this:</p>

<pre><code>start: 2008 2008.0
end: 2008 478.0 -1530.0
478
</code></pre>

<p>So calling <code>child.getTop()</code> after the animation is complete and setting a new position returns a positive integer, but the view is not actually completely on screen. It is above the screen, partly visible. The height of the view itself is about 700px. I am still so confused as to why this is such a hard thing to accomplish.</p>

<p><strong>EDIT 2</strong></p>

<p>I have also tried setting layoutparams inside the <code>onAnimationEnd</code> method:</p>

<pre><code>RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin = (int)child.getY();
child.setLayoutParams(params);
</code></pre>

<p>Result: <code>child.getTop()</code> still returns the original position of 2008.</p>