gpt4 book ai didi

android - 删除 View 上的 OnClickListener 删除后面 View 上的所有触摸事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:02 25 4
gpt4 key购买 nike

我有一个带有标题、垂直菜单和透明背景 View 的顶部栏布局。

当按下 btn_menu 时,将使用动画打开垂直菜单。当菜单打开时,我在透明背景 View 上设置了一个 OnClickListener,它会在单击透明背景时关闭菜单。关闭菜单时,我从背景 View 中删除了 OnClickListener,使用:

mTopBarBg.setOnClickListener(null);

问题是它似乎删除了它后面的 View 的所有触摸事件(在主布局的 content_container 中设置)。例如。一个不再检测滑动的 ViewPager,或者一个不再滚动且不能再被点击的 ListView,而它们之前可以正常工作。

怎么了?

在顶部条形 fragment 中

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>

打开菜单的顶部栏

enter image description here

最佳答案

尝试在叠加层 View 上也使用 setClickable(false)。使用 setOnClickListener() 方法使 View 可点击,这可能会“吃掉”您 future 的事件(即使在使用 null 之后也不会修改之前设置的可点击属性)。

另一方面,相对于您尝试做的事情,您的布局非常复杂。更准确地说,布局比它应该的更深。如果 FrameLayout 容器具有与栏的 RelativeLayout 相同的属性(您可以使用 include 标记来包含栏而不需要额外的 FrameLayout)。您也可以丢失 top_bar_bg View 并直接在根 RelativeLayout 上设置监听器。最后,内部 RelativeLayout(header) 可以被移除,并通过将 subview 正确定位在根 Relativelayout 中,用一个白色的空 View 替换它们(具有适当的尺寸来模拟白色背景)。

关于android - 删除 View 上的 OnClickListener 删除后面 View 上的所有触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16066235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com