Android: unable to click the bottom TextView after translate animation within a FrameLayout

烈酒焚心 提交于 2019-12-11 09:08:03

问题


I have a FrameLayout in which I have placed two identical TextViews. I want to be able to translate the first view to the left (which I have done and is working like a charm). However I want to be able to click the TextView underneath it to perform an action.

When I try to click the bottom TextView, the top TextView gets clicked again instead. I have a feeling this is because the way animations are rendered and the change in actual x,y position doesn't take effect.

This is what I have so far.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="@string/hello" 
        android:id="@+id/unbutt"
        android:gravity="right|center_vertical"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="@string/hello" 
        android:id="@+id/butt" />

</FrameLayout>

The code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class Main extends Activity implements AnimationListener, OnClickListener
{
    /** Called when the activity is first created. */

    private class BottomViewClick implements OnClickListener
{

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "Second Click", 5).show();
    }

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.butt); 
    tv.setBackgroundColor(0xffb71700);
    tv.setOnClickListener(this);

    TextView tv2 = (TextView)findViewById(R.id.unbutt); 
    tv2.setBackgroundColor(0xffb700ff);
    tv2.setOnClickListener(new BottomViewClick());

}

    private boolean revealed = false;

    @Override
    public void onClick(View v) {
        Animation a ;
        if(!revealed)
            a = new TranslateAnimation(0f, -200f, 0f, 0f);
        else
            a = new TranslateAnimation(-200f, 0f, 0f, 0f);
        a.setDuration(500);
        a.setFillAfter(true);
        a.setAnimationListener(this);
        v.startAnimation(a);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if(revealed)
            revealed = false;
        else
            revealed = true;
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }
}

回答1:


You have ICS in the tag, so I assume that's what your target is. In that case, the Animation objects you're using are essentially deprecated in favor of the Animator class. The old way of doing it only moves the visual location of the View while the physical location remains the same. You would have to move it yourself by manipulating the margins of the view. Using an ObjectAnimator on the other hand allows you to physically move the object along with it's visual component.

http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html



来源:https://stackoverflow.com/questions/9182893/android-unable-to-click-the-bottom-textview-after-translate-animation-within-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!