我有一个Activity,其中包含一个ViewPager。在这个ViewPager中有四个Fragments。其中一个Fragment(我们称为MyListFragment)具有一个布局文件,其中包含一个ListView和一个MapView(Google maps V2),放置在RelativeLayout中。只有这两个视图之一可见。最初,ListView是可见的,而MapView是隐藏的。我有一个按钮,当我单击它时,我会切换可见性:ListView变为隐藏,MapView变为可见。当我再次单击它时,MapView设置为隐藏,ListView设置为可见。你明白了吧。
现在的问题是:在将MapView设置为隐藏并将ListView恢复为可见后,ViewPager将无法再滑动。相反,所有触摸事件都转到ListView上(这是一个普通的垂直ListView)。我分成几个步骤,并检查何时可以或不能滑动ViewPager。
- ListView可见,MapView不可见:一切正常。
- 将ListView设置为不可见:一切正常。
- 将ListView恢复为可见:一切正常。
- 将ListView设置为不可见,将MapView设置为可见,然后将MapView设置为不可见:一切正常。
- 将ListView设置为可见:!!! ViewPager不再在此Fragment上滑动!!!
- 将ListView设置为不可见:一切正常。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/fragment_padding"
android:paddingRight="@dimen/fragment_padding"
android:paddingBottom="@dimen/list_padding_bottom"
android:clipToPadding="false"
android:scrollbarStyle="outsideOverlay"/>
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<Button
android:id="@+id/button_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/fragment_padding"
android:layout_marginLeft="@dimen/fragment_padding"
android:layout_marginRight="@dimen/fragment_padding"
android:text="Show on map"/>
以下是一些onCreateView
代码:
this.listView = (ListView) view.findViewById(R.id.listView);
this.mapView = (MapView) view.findViewById(R.id.mapView);
this.mapView.onCreate(savedInstanceState);
this.mapView.getMapAsync(this);
这是按钮的onClick事件中发生的事情:
if(this.listView.getVisibility() == View.VISIBLE)
{
this.listView.setVisibility(View.GONE);
this.mapView.setVisibility(View.VISIBLE);
this.buttonSwitch.setText("Show in list");
}
else
{
this.listView.setVisibility(View.VISIBLE);
this.mapView.setVisibility(View.GONE);
this.buttonSwitch.setText("Show on map");
}
如果我需要提供另一段代码,请告诉我。任何帮助都将不胜感激,因为我似乎找不到这个错误的原因。