后台ListView接收输入(Fragment API)
我的应用程序基于选项卡式布局,其中每个选项卡都分配一个 FragmentActivity。
其中一项活动具有以下布局:
<FrameLayout
android:id="@+id/filialenView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_height="wrap_content" android:id="@+id/filialList"
android:layout_width="fill_parent"></ListView>
</FrameLayout>
切换到该选项卡时,将创建并显示一个列表。
如果选择了一个列表项,则会创建一个 Fragment 并显示在列表顶部:
Filiale fragment = new Filiale();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.filialenView, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Fragment Filiale 的布局“filial_detail.xml”看起来像这样
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... content...
</LinearLayout>
,并在 onCreateView 中膨胀,就像这样
View curView = inflater.inflate(R.layout.filial_detail, container, false);
一切都按预期工作,除了切换到片段后,不再可见的列表似乎保持输入焦点。如果我触摸屏幕上的某个“空白”区域,则会触发片段后面的隐藏列表项。另外,如果我滑动,我可以从 LogCat 输出中看到列表滚动。
根据我对FrameLayout的理解,像这样堆叠视图应该没问题。
该代码有什么问题?
由于还没有答案或评论,一个更普遍的问题:
是否存在任何已知的情况,后台视图应该接收输入,或者在任何情况下都是错误的行为?
My app is based on a tabbed layout where each tab is assignes a FragmentActivity.
One of this activities has the following layout:
<FrameLayout
android:id="@+id/filialenView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_height="wrap_content" android:id="@+id/filialList"
android:layout_width="fill_parent"></ListView>
</FrameLayout>
When switching to that tab, a list is created and shown.
If a list item is selected, a Fragment is created and shown on top of the list:
Filiale fragment = new Filiale();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.filialenView, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
The layout "filial_detail.xml" for Fragment Filiale looks like
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
... content...
</LinearLayout>
and is inflated in onCreateView like this
View curView = inflater.inflate(R.layout.filial_detail, container, false);
Everything works like expected, except that after switching to the Fragment, the nomore visible list seems to keep input focus. If I touch the screen in some "empty" area, the hidden list item behind the fragment triggers. Also, if I swipe, I can see from LogCat output, that the list scrolls.
Based on my understanding of FrameLayout, it should be ok to stack views like this.
What's wrong with that code?
Since there's no answer or comment yet, a more general question:
Is there any known situation where a view in background is supposed to receive input or is that wrong behaviour in any case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出现这种行为的原因是 ViewGroups 默认情况下不处理输入事件,而是将它们传递给它们后面的小部件(如果有)。
在我给定的代码中,列表的高度为“wrap_content”,因此剩余的垂直区域仅由将输入传递到隐藏片段的 LinearLayout 元素覆盖。
我想要的行为可以通过
或
The reason for this behaviour is that ViewGroups by default don't handle input events, but pass them through to widgets behind them, if there are any.
In my given code, the list has height "wrap_content", thus the remaining vertical area is covered only by the LinearLayout element which passes input through to the hidden fragment.
The behaviour I wanted can be achieved by
or