开发者

ListView in background receives input (Fragment API)

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 开发者_JAVA百科known situation where a view in background is supposed to receive input or is that wrong behaviour in any case?


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

  • setting the height of the listView to "fill_parent" or
  • adding an OnClickListener to the LinearLayout with an empty onClick method
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜