Fragment穿透事件处理,点击当前fragment空白区域触发了其他fragment控件的点击事件

一般项目都是这样的,下面一个底部导航,然后切换不同的fragment,我这边是加载所有的fragment,然后让他们显示或者隐藏 。替换的话也可以,但是那样体验和性能都不太好。但是显示和隐藏的时候遇到了一个奇怪的问题,前一个fragment隐藏的时候在后一个fragment中竟然出现了前一个fragment的点击事件。也就是说fragment在隐藏的状态下的点击事件也是可以出发的。主要是因为当前的fragment有一大片的空白区域 导致的 ,经过一番思索  我想这要是让这个空白区域可点击,然后不做任何处理不就完了。

最后处理:在fragment的根部局加上    android:clickable="true"  这个属性  直接搞定。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,可以使用 Fragment 来隐藏 Activity 中设置的件。具体步骤如下: 1. 在 Activity 的布局文件中,使用 `<fragment>` 标签来添加 Fragment。 2. 在 Fragment 的布局文件中,添加需要隐藏的件。 3. 在 Fragment 中使用 `getView()` 方法获取 Fragment 的根视图,并使用 `setVisibility()` 方法将需要隐藏的件设置为 `View.GONE`。 举个例子: Activity 布局文件: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/my_fragment" android:name="com.example.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/my_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_centerInParent="true" /> </RelativeLayout> ``` Fragment 布局文件: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/hide_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide TextView" /> </LinearLayout> ``` Fragment 代码: ``` public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); Button hideButton = (Button) view.findViewById(R.id.hide_button); hideButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { View textView = getView().findViewById(R.id.my_textview); textView.setVisibility(View.GONE); } }); return view; } } ``` 在上面的例子中,当用户点 Fragment 中的按钮时,`TextView` 将被隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值