2013-04-21 110 views
0

我有一个带有顶栏容器和内容容器的布局。点击顶部栏中的按钮时,会使用动画显示垂直菜单。我的minSdkVersion是9.只有当我触摸屏幕后才开始动画

这适用于当我启动应用程序,我仍然没有点击菜单按钮(即内容片段没有改变),但只要我点击一个选项(然后替换content_container中的片段),垂直菜单行为不正常。菜单btn的点击事件被正确触发,但垂直菜单并不总是显示(但有时候是......)。然而,当我点击按钮然后触摸屏幕时,动画(显示或隐藏菜单)开始。

我想它与垂直菜单重叠内容片段有关,然后替换内容片段以某种方式修改它,但我找不到任何解决方案。

有人可以帮忙吗?

顶杆片段

@Override 
    public void onActivityCreated (Bundle savedInstanceState){ 

     super.onActivityCreated(savedInstanceState); 

     toggleMenu(0);  

     Button btn_menu = (Button) getView().findViewById(R.id.btn_menu); 
     btn_menu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mVerticalMenu.setVisibility(View.VISIBLE); 
       toggleMenu(1000); 
      } 
     }); 
    } 

    private void toggleMenu(int duration){ 
     if(mMenuIsOpen){ 

      TranslateAnimation anim1 = new TranslateAnimation(0,0,0,-(mHeight-mMenuVerticalOffset)); 
      anim1.setFillAfter(true); 
      anim1.setDuration(duration); 
      mVerticalMenu.setAnimation(anim1); 

      AlphaAnimation anim2 = new AlphaAnimation(0.7f, 0.0f); 
      anim2.setFillAfter(true); 
      anim2.setDuration(duration);    

      menu_option_01.setOnClickListener(null); 
      menu_option_02.setOnClickListener(null); 
      menu_option_03.setOnClickListener(null); 

      mMenuIsOpen = false; 
     } 
     else{ 

      TranslateAnimation anim1 = new TranslateAnimation(0,0,-(mHeight-mMenuVerticalOffset),0); 
      anim1.setFillAfter(true); 
      anim1.setDuration(duration); 
      mVerticalMenu.setAnimation(anim1); 

      AlphaAnimation anim2 = new AlphaAnimation(0.0f, 0.7f); 
      anim2.setFillAfter(true); 
      anim2.setDuration(duration); 

      menu_option_01.setOnClickListener(mButtonClickListener); 
      menu_option_02.setOnClickListener(mButtonClickListener); 
      menu_option_03.setOnClickListener(mButtonClickListener); 

      mMenuIsOpen = true; 
     } 

    } 



    private OnClickListener mButtonClickListener = new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      toggleMenu(1000); 

      if(!v.isSelected()){   


       FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); 

       switch(v.getId()){ 

       case R.id.menu_option_01: 

        // replace content_container by fragment 1 

        break; 

       case R.id.btn_02: 

        // replace content_container by fragment 2 

        break;  

       case R.id.btn_03: 

        // replace content_container by fragment 3 

        break;  

       } 
      } 
     } 

    }; 

    private OnClickListener mBgClickListener = new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      toggleMenu(1000);   
     } 
    }; 

主要布局

<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" > 

    <FrameLayout 
     android:id="@+id/content_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingTop="44dp" /> 

    <FrameLayout 
     android:id="@+id/top_bar_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clipChildren="false" /> 

</RelativeLayout> 

顶部条布局

<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:background="#00000000" > 

    <LinearLayout 
     android:id="@+id/vertical_menu" 
     android:layout_width="50dp" 
     android:layout_height="match_parent" 
     android:layout_marginTop="44dp" 
     android:background="#ffffff" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <!-- menu layout --> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="44dp" 
     android:background="#ffffff" > 

     <Button 
      android:id="@+id/btn_menu" 
      android:layout_width="50dp" 
      android:layout_height="44dp" 
      android:background="@drawable/menubtn" /> 

     <ImageView 
      android:layout_width="130dp" 
      android:layout_height="44dp" 
      android:src="@drawable/logo" 
      android:layout_alignParentRight="true" /> 
    </RelativeLayout> 

</RelativeLayout> 

enter image description here

+0

只是澄清:起初菜单很好。您打开菜单并选择一个将触发内容片段替换的项目(此时菜单已关闭,对不对?)。然后再次单击菜单按钮(不打开?但如果您单击背景,菜单打开?) – Luksprog 2013-04-22 07:07:47

+0

首先,菜单起作用。然后,内容修改后,只有在点击后我触摸屏幕的任何地方,点击菜单按钮后菜单才会打开/关闭。我找到了一个解决方案(请参阅我的答案)。 – jul 2013-04-22 07:15:32

回答

1

在我Toggle方法的末尾,我无效根视图:

rootView.invalidate(); 

,现在它的工作原理。不太清楚为什么我必须这样做,虽然...

+0

我知道这是旧的,但我有同样的问题...任何新的方式来处理这个? – Motheus 2014-11-09 00:45:33

1

我知道已经有一个公认的答案,但我有一个类似的问题,答案没有帮助。

我有一个看法,我宣布在我的布局结束,以保持其Z指数高于其兄弟姐妹。我必须触摸该页面才能使动画生效。

所以我通过Java再次设置Z指数,它工作。

view.bringToFront(); 
+0

我有同样的问题,并试图在动画被称为之前添加这个,但没有运气......是否有任何新的方法来处理这个? – Motheus 2014-11-09 00:55:24

相关问题