【问题标题】:Adding a fragment to an array on a parent scrollview将片段添加到父滚动视图上的数组
【发布时间】:2015-05-05 11:01:32
【问题描述】:

我需要将 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 上添加片段,但视图仍然是“空”:

@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());
}

【问题讨论】:

    标签: android android-fragments scrollview


    【解决方案1】:

    尝试使用 ViewTreeObserver:

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

    【讨论】:

    • 谢谢,我去看看。目前我刚刚使用动作识别器实现了一个透明图像叠加,但这似乎不适用于旧版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多