SupportMapFragment无法强制转换为从SupportMapFragment

时间:2017-03-29 18:32:34

标签: android supportmapfragment

在我的活动中,我想在GoogleMap中使用NestedScrollView,因此我创建了一个Class : MySupportMapFragment,以便我的触摸事件能够很好地适用于NestedScrollView中的地图。 但是当我将SupportMapFragment转换为MySupportMapFragment时,我的应用程序崩溃了。

代码: MySupportMapFragment:

    public class MySupportMapFragment extends SupportMapFragment {

        private OnTouchListener mListener;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

            View layout = super.onCreateView(inflater, parent, savedInstanceState);

            TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
            frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            ((ViewGroup) layout).addView(frameLayout,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            return layout;
        }

        public void setListener(OnTouchListener listener) {
            mListener = listener;
        }

        public interface OnTouchListener {
            public abstract void onTouch();
        }

        public class TouchableWrapper extends FrameLayout {

            public TouchableWrapper(Context context) {
                super(context);
            }

            @Override
            public boolean dispatchTouchEvent(MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mListener.onTouch();
                        break;
                    case MotionEvent.ACTION_UP:
                        mListener.onTouch();
                        break;
                }
                return super.dispatchTouchEvent(event);
            }
        }
    }


inside Activity extending from `AppCompatActivity` 

MySupportMapFragment mapFragment;
        mapFragment = (MySupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        if(mapFragment != null)
            mapFragment.setListener(new MySupportMapFragment.OnTouchListener() {
                @Override
                public void onTouch() {
                    nv.requestDisallowInterceptTouchEvent(true);
                }
            });

        mapFragment.getMapAsync(this);

XML:

<root>
.....
....

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/map"
                            android:name="com.google.android.gms.maps.SupportMapFragment"

                            />

....
</root>

0 个答案:

没有答案