点击片段会调用它背后的活动

And*_*cko 15 android android-listview android-fragments

我猜它的东西很简单,也许是listview或片段中的设置.但是我现在几个小时都找不到它的解决方案.那么..我有一个像这样的listView

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

    ft.addToBackStack(null);
    ft.commit();
Run Code Online (Sandbox Code Playgroud)

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

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

为什么这个片段是透明的?我怎样才能解决这个问题?

Mar*_*lia 16

我通过添加android:clickable="true"行为就像是"透明"的元素来解决类似的问题.使其可点击确保点击事件由它处理而不是传递给下面的视图.

  • 这样做的问题是它不会停止空白区域的触觉反馈。 (2认同)

Kar*_*oly 10

所以,我遇到了完全相同的问题,我仍然不知道原因,但请确保将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>
Run Code Online (Sandbox Code Playgroud)


Dee*_*eeV 6

我认为问题在于该replace方法不能取代ListView.它只替换容器中的其他片段.在这种特定情况下没有.因此,您实际上是在片段之上添加片段,ListView因为它RelativeLayout允许视图在彼此之上.当你点击片段时,你没有完全处理它,所以它会进入基本视图ListView.

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

  • @艾哈迈德。问题在于 Fragment 中的 View 无法捕获触摸,因此触摸会沿着 View 层次结构向下移动,直到找到可以捕获触摸的内容。一个有点丑陋的解决方案是将“onTouchListener”设置为“Fragment”的根“View”,它只返回“true”。当单击此片段时,这将阻止所有触摸的内容进入其他视图。 (2认同)