【问题标题】:clicking on the fragment invokes activity behind it单击片段会调用其背后的活动
【发布时间】:2012-02-29 17:49:28
【问题描述】:

我猜它很简单,可能是列表视图或片段中的设置。 但是我现在几个小时都找不到解决方案。 所以.. 我有一个这样的列表视图

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true">

<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#fffafa"
    />

</RelativeLayout>

当有人点击列表项时。我用这样的片段替换它

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content, newFragment);

    ft.addToBackStack(null);
    ft.commit();

如果我单击列表上的项目,整个列表将替换为由一个按钮和几个文本字段组成的片段(*)。 Fragment 用有关项目的信息正确替换了 listView。当我按下返回时,它确实会正确地返回列表视图。

问题是,如果我点击 * 的背景,它的行为就像我点击它后面的列表一样......并将关于该项目的片段带到前面。我可以无休止地点击背景以带来其他片段。我可以通过点击返回,返回来挖掘列表。

为什么这个片段是透明的?我该如何解决这个问题?

【问题讨论】:

    标签: android android-listview android-fragments


    【解决方案1】:

    我通过在表现得好像“透明”的元素中添加android:clickable="true" 解决了类似的问题。使其可点击确保点击事件由它处理,而不是传递给下面的视图。

    【讨论】:

    • 问题在于它不会停止对空白区域的触觉反馈。
    【解决方案2】:

    所以,我遇到了完全相同的问题,我仍然不知道原因,但请确保将 android:clickable="true" 添加到第二个片段布局的 roovView .

    类似这样的:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:clickable="true"
    android:orientation="vertical">
    .....any content for your item (2nd fragment).....
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      我认为问题在于replace 方法不能替代ListView。它只替换容器内的其他片段。在这种特定情况下,没有。因此,您实际上是将片段添加到ListView 之上,因为RelativeLayout 允许视图彼此叠加。当您单击片段时,您并没有完全处理它,因此它会进入基本视图,即ListView

      一个简单、快速和肮脏的解决方案是检索ListView 对象,然后自己将其从容器中删除或将其可见性设置为GONE。更好的解决方案是使用ListFragment,它应该会显示预期的结果。

      【讨论】:

      • 我重新设计了它,以便列表是一个起始片段。然后覆盖 obBackPressed 以便它阻止将此列表从片段 backstack 中取出 if (manager.getBackStackEntryCount() == 1) 并且它工作得很好。谢谢!
      • 我遇到了同样的问题,但是所有的碎片。我以与添加 ListView 相同的方式添加片段:通过 xml。我切换到使用 FragmentManager 为片段充气,现在一切正常。所以问题是没有被 FragmentManager 添加的任何东西都不会被删除。
      • 如果你没有listfragment,那么一个人应该怎么做?
      • @Ahmed。问题是 Fragment 中的 View 没有捕捉到触摸,所以触摸沿着 View 层次结构向下移动,直到找到可以捕捉到的东西。一个有点丑陋的解决方案是将onTouchListener 设置为Fragment 的根View,它只返回“true”。当点击这个片段时,这将阻止所有触摸到其他视图。
      • 这可以解决问题,但是当您打开对讲时,它仍然会读取后面的屏幕。另一个需要处理的潜在问题。
      【解决方案4】:

      将 clickable 属性设置为 true,以便单击事件仅在前景片段上使用。

          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:clickable="true" />
      

      【讨论】:

        【解决方案5】:

        在前景片段的 FrameLayout 标签中设置 android:clickable="true"

        它对我有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-20
          • 1970-01-01
          相关资源
          最近更新 更多