在另一个片段onClickListener问题顶部添加一个片段 [英] Adding a fragment on top of another fragment onClickListener issue

查看:180
本文介绍了在另一个片段onClickListener问题顶部添加一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的添加一个片段的活动,而不是替换的当前片段(因为此相对应的行为,我想拥有的类型)。

我的问题是点击顶部片段(所述一个那当前可见),其中在非可见片段的视图位于一个斑点,使onClick事件在第二,不可见的片段的视图, 开火。 为什么会出现这种情况,并哪能prevent呢?

这是在code我用先添加ListView的片段活动:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    ...

    如果(savedInstanceState == NULL){
        listFragment =新ListFragment();
        getSupportFragmentManager()的BeginTransaction()
                。新增(R.id.frame_container,listFragment)
                .addToBackStack(listFragment .TAG)
                。承诺();
    }

    ...

}
 

在此相同的活动我添加第二个片段,在片段列表顶部:

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,
                                意图数据){
    ...
            createItemFragment =新CreateItemFragment();
            getSupportFragmentManager()的BeginTransaction()
                    。新增(R.id.frame_container,createItemFragment)
                    .addToBackStack(createItemFragment.TAG)
                    。承诺();
    ...
}
 

解决方案

您只需以下属性添加到片段的XML根布局agoes之上 -

 机器人:可点击=真
 

这将确保触摸事件将不高于顶层传播进一步

I'm adding a fragment to an activity instead of replacing the current fragment (because this corresponds to the type of behavior I want to have).

My problem is that clicking in a spot on the top fragment (the one that's currently visible), where a view in the non-visible fragment is located, causes an onClick event on the view in the second, non-visible fragment, to fire. Why is this happening and how can I prevent this?

This is the code I use to first add the ListView fragment to the activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    if (savedInstanceState == null) {
        listFragment = new ListFragment ();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame_container, listFragment)
                .addToBackStack(listFragment .TAG)
                .commit();
    }

    ...

}

In this same activity I'm adding the second fragment, on top of the list fragment:

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    ...
            createItemFragment = new CreateItemFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.frame_container, createItemFragment)
                    .addToBackStack(createItemFragment.TAG)
                    .commit();
    ...
}

解决方案

You can just add the following attribute to the XML root layout of the fragment that agoes on top-

android:clickable="true"

This will ensure that touch events will not propagate further than the top layer.

这篇关于在另一个片段onClickListener问题顶部添加一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆