7

ヘッダー、垂直メニュー、透明な背景ビューを備えたトップ バー レイアウトがあります。

btn_menu押すと、アニメーションを使用して垂直メニューが開きます。メニューが開いているOnClickListenerときに、透明な背景をクリックするとメニューが閉じる透明な背景ビューを設定します。メニューを閉じるときは、次OnClickListenerを使用してバックグラウンド ビューから削除します。

mTopBarBg.setOnClickListener(null);

問題は、その背後にあるビューのすべてのタッチ イベントを削除するように見えることです (content_containerメイン レイアウトの で設定)。例えば。ViewPagerスワイプを検出しなくなったり、以前ListViewは正しく機能していたのに、スクロールせず、クリックできなくなったりします。

どうしたの?

トップバーのフラグメント

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);
        mTopBarBg.setAnimation(anim2);

        mTopBarBg.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);
        mTopBarBg.setAnimation(anim2);

        mTopBarBg.setOnClickListener(mBgClickListener);

        mMenuIsOpen = true;
    }
}

メインレイアウト

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/mainbg" 
        android:scaleType="centerCrop"/>

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

    <View
        android:id="@+id/top_bar_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:visibility="gone" />

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

        <!-- vertical menu layout -->

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/header"
        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" />       

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_toRightOf="@id/btn_menu"
            android:gravity="center" >

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

</RelativeLayout>

メニューが開いているトップバー

ここに画像の説明を入力

4

2 に答える 2

1

削除する代わりにonClickListenerできることは 1 つです。常に を設定し、onClickListenera を使用しboolean variableてメニューのオンとオフをトレースします。したがって、コードは次のようになります

 boolean is_menu_open = false;
 public void onClick(View v){
        if(is_menu_open){
             hidemenu;
             is_menu_open = false;
        } else
             do nothing
  }

メニューを表示するたびに、に設定is_menu_openしますtrue

于 2013-04-22T04:14:35.467 に答える