将片段添加到父卷轴视图

时间:2015-05-05 11:01:32

标签: android android-fragments scrollview

我需要将MapView添加到例外列表中,以便当用户触摸地图视图时滚动视图不会滚动。

<?xml version="1.0" encoding="utf-8"?>

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

    <org.xxx.ScrollViewWithMap
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical"
        android:id="@+id/base_scrollview">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/content_fragment"
                android:layout_marginBottom="5dp" />

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/map_fragment"
                android:layout_margin="10dp" />
        </LinearLayout>
    </org.xxx.ScrollViewWithMap>


</RelativeLayout>

在基础片段中,mapFragment通过以下方式添加:

        LocationMapFragment locationMapFragment = new LocationMapFragment();
        locationMapFragment.setArguments(args);
        getChildFragmentManager().beginTransaction().add(R.id.map_fragment, locationMapFragment).commit();

现在我需要将此视图添加到ScrollViewWithMap中的数组,但是我该怎么做呢。在基本片段'locationMapFragment'中为getView()返回null,我不知道如何从片段中访问scrollView。

[编辑:] 我确实尝试在onAttach上添加片段,但视图仍为“null”:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }

    ScrollViewWithMap scrollview = (ScrollViewWithMap) activity.findViewById(R.id.base_scrollview);
    scrollview.addInterceptScrollView(this.getView());
}

1 个答案:

答案 0 :(得分:0)

尝试使用ViewTreeObserver:

ViewTreeObserver observer = yourView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //add in list here

            }
        });