2013-04-21 37 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

相關問題