2014-03-24 64 views
2

所以,我有一个应用程序,当用户点击一个button将与按钮的layout(如sliding menu)做一个animation,然后,如果他打另一个按钮,它具有使看不见或者走了第一个布局,然后是新的布局。View.GONE不工作后,翻译动画

但是,当我试图使AnimationStart我的layout与按钮不可见,它不这样做。

我已经尝试了一些解决方案,从这里开始:

Why doesn't setvisibility work after a view is animated

Setvisibilityview Gone doesn't disappear a view

但是毫无效果!

任何帮助?

Java code(它是两个按钮相同)

btn_home1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

        layout1.setVisibility(View.VISIBLE); 
        btn_home.setVisibility(View.VISIBLE); 
        btn_book.setVisibility(View.VISIBLE); 
        btn_find_us.setVisibility(View.VISIBLE); 
        btn_menu.setVisibility(View.VISIBLE); 

       TranslateAnimation slide = new TranslateAnimation(-100, 0, 0,0); 
       slide.setDuration(1000); 
       slide.setFillAfter(true); 
       slide.setAnimationListener(new Animation.AnimationListener() { 
        @Override 
        public void onAnimationStart(Animation animation) { 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
        btn_home2.setVisibility(View.GONE); 
        btn_book2.setVisibility(View.GONE); 
        btn_find_us2.setVisibility(View.GONE); 
        btn_menu2.setVisibility(View.GONE); 
         layout2.setVisibility(View.GONE); 
     } 
    }, 0); 
          btn_home.setClickable(false); 
          btn_book.setClickable(false); 
          btn_find_us.setClickable(false); 
          btn_menu.setClickable(false);       
        } 

        @Override 
        public void onAnimationEnd(Animation animation) { 
          btn_home.setClickable(true); 
          btn_book.setClickable(true); 
          btn_find_us.setClickable(true); 
          btn_menu.setClickable(true);  
        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { 

        } 
       }); 
       btn_menu.startAnimation(slide); 
       btn_book.startAnimation(slide); 
       btn_find_us.startAnimation(slide); 
       btn_home.startAnimation(slide); 
       layout1.startAnimation(slide); 
       } 
     }); 

XML代码:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="#BE2625" > 
      <Button 
       android:id="@+id/btn_home1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="150dp" 
       android:text="342" 
       /> 

       <Button 
       android:id="@+id/btn_home11" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="250dp" 
       android:text="34243" 
       /> 

    <LinearLayout 
     android:id="@+id/lala" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#80000000" 
     android:visibility="gone" 
     android:orientation="vertical"> 

     <Button 
       android:id="@+id/btn_home" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
       /> 
      <Button 
       android:id="@+id/btn_book" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 
      <Button 
       android:id="@+id/btn_find_us" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 
      <Button 
       android:id="@+id/btn_menu" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/lala1" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#80000000" 
     android:visibility="gone" 
     android:orientation="vertical"> 

     <Button 
       android:id="@+id/btn_home2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
       /> 
      <Button 
       android:id="@+id/btn_book2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 
      <Button 
       android:id="@+id/btn_find_us2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 
      <Button 
       android:id="@+id/btn_menu2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

    </LinearLayout> 
</RelativeLayout> 
+0

你在哪里设置视图在动画后不可见? – Whitney

+0

更新!我在AnimationStart上执行它,因此布局和按钮都可以隐藏或消失。 – pap

回答

0

我又做了这个链接研究:Why doesn't setVisibility work after a view is animated?

而且找到@Chris Knight的答案:

Another way to work around this is to wrap your animated view in another view and set the visibility of that wrapper view.

所以我作为他做了两FrameLayout,然后设置为setVisibility(View.GONE)一次一个,因为用户会在同一时间击中一个按钮,它会同时打开一个Slide Menu

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="#BE2625" > 
      <Button 
       android:id="@+id/btn_home1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="150dp" 
       android:text="342" 
       /> 

       <Button 
       android:id="@+id/btn_home11" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="250dp" 
       android:text="34243" 
       /> 
    <FrameLayout 
     android:id="@+id/lsd1" 
     android:layout_height="match_parent" 
     android:layout_width="240dp"> 

    <LinearLayout 
     android:id="@+id/lala" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#80000000" 
     android:visibility="gone" 
     android:orientation="vertical"> 

     <Button 
       android:id="@+id/btn_home" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
       /> 

      <Button 
       android:id="@+id/btn_book" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

      <Button 
       android:id="@+id/btn_find_us" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

      <Button 
       android:id="@+id/btn_menu" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 


    </LinearLayout> 
</FrameLayout>  

     <FrameLayout 
     android:id="@+id/lsd2" 
     android:layout_height="match_parent" 
     android:layout_width="240dp"> 
    <LinearLayout 
     android:id="@+id/lala1" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#80000000" 
     android:visibility="gone" 
     android:orientation="vertical"> 

     <Button 
       android:id="@+id/btn_home2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
       /> 

      <Button 
       android:id="@+id/btn_book2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

      <Button 
       android:id="@+id/btn_find_us2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 

      <Button 
       android:id="@+id/btn_menu2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.25" 
        /> 


    </LinearLayout> 
</FrameLayout>  

</RelativeLayout>