无法刷新FrameLayout?

时间:2016-08-29 07:24:07

标签: java android view android-framelayout ondraw

我开始研究Android开发,但我无法正确刷新我的应用FrameLayout

FrameLayout(panelA)持有StreetViewPanorama片段,每次从FrameLayout删除片段并尝试添加另一个片段后,整个FrameLayout冻结显示最后一个片段图像。

您可以在下面看到我的代码

    package com.example.shanx.project_track_it_v001;

    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import com.example.shanx.utilities.MapHandler;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
    import com.google.android.gms.maps.StreetViewPanorama;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
                                                          OnStreetViewPanoramaReadyCallback {

        private static final String INITIAL_POSITION = "Rio de Janeiro";

        //Application's map handler
        private MapHandler mHandler = new MapHandler();

        //Application's map
        private static GoogleMap mMap;

        //Application's street view
        private static StreetViewPanorama mStreetView;

        //Application's map activity
        public static MapsActivity mActivity;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            //Set the Activity's layout as the content view
            setContentView(R.layout.activity_maps);

            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

            //Set (a instance of) the callback on the fragment
            mapFragment.getMapAsync(this);

            mActivity = this;

        }   

        @Override
        public void onMapReady(GoogleMap googleMap) {

            double latitudeArray[] = {24.345, -20.7444, 56.877, -79.2762, 77.128, 69.12, -32.111, 45.785, 77.896, -56.7888};
            double longitudeArray[] = {-124.345, -80.7444, 116.877, 33.2762, 177.128, 52.4567, -132.111, 25.785, 97.896, -156.7888};

            mMap = googleMap;
            mHandler.setMapType(mMap, GoogleMap.MAP_TYPE_NORMAL);

            // Add a marker in Rio de Janeiro and move the camera
            LatLng initialPos = new LatLng(-22.907166,-43.176911);

            mMap.addMarker(new MarkerOptions().position(initialPos).title("Marker in " + INITIAL_POSITION));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(initialPos));

            //PolylineEncoder.printData(latitudeArray, longitudeArray);

            mMap.setPadding(0, 0, 0, 100);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.getUiSettings().setAllGesturesEnabled(true);
            try {
                mMap.setMyLocationEnabled(true);
            }
            catch(SecurityException e){
                e.printStackTrace();
            }
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
            mMap.getUiSettings().setCompassEnabled(true);
            mMap.setBuildingsEnabled(true);
        }

        @Override
        public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {

            mStreetView = streetViewPanorama;
            mStreetView.setPosition(new LatLng(-22.907166,-43.176911));

        }

        public void buttonPressed(View buttonPressed){

            mHandler.showStreetView(this);

        }

        public static GoogleMap getMap(){

            return mMap;

        }

        public static StreetViewPanorama getStreetView(){

            return mStreetView;

        }

    }

这是另一个班级:

    package com.example.shanx.utilities;
    import android.app.FragmentTransaction;
    import android.graphics.Canvas;
    import android.util.Log;
    import android.view.View;
    import com.example.shanx.project_track_it_v001.MapsActivity;
    import com.example.shanx.project_track_it_v001.R;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.StreetViewPanoramaFragment;
    import com.google.android.gms.maps.StreetViewPanoramaOptions;

    public final class MapHandler {

        private boolean isStreetViewLoaded = false;
        private StreetViewPanoramaFragment streetViewFragment;
        private FragmentTransaction fragmentTransaction;

        public void setMapType(GoogleMap map, int type){

            switch(type){
                case GoogleMap.MAP_TYPE_NONE:
                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    break;
                case GoogleMap.MAP_TYPE_TERRAIN:
                    map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                    break;
                case GoogleMap.MAP_TYPE_SATELLITE:
                    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                    break;
                case GoogleMap.MAP_TYPE_HYBRID:
                    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    break;
                default: map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }

        }

        public void showStreetView(MapsActivity activity) {

            if (!isStreetViewLoaded)
                addStreetView(activity);
                else removeStreetView(activity);

        }

        private void addStreetView(MapsActivity activity){

            if((View view = activity.findViewById(R.id.panelA)) != null){
                  view.setVisibility(View.VISIBLE);
                  view.invalidate();   
            }    

            StreetViewPanoramaOptions streetViewOptions = new StreetViewPanoramaOptions();
            streetViewOptions.panningGesturesEnabled(true);
            streetViewOptions.zoomGesturesEnabled(true);
            streetViewOptions.streetNamesEnabled(true);

            streetViewFragment = StreetViewPanoramaFragment.newInstance(streetViewOptions);
            fragmentTransaction = activity.getFragmentManager().beginTransaction();
            //panelA is the FrameLayout that I want to show another fragment
            fragmentTransaction.add(R.id.panelA, streetViewFragment);
            fragmentTransaction.commit();

            streetViewFragment.getStreetViewPanoramaAsync(activity);

            isStreetViewLoaded = true;

        }

        private void removeStreetView(MapsActivity activity){

            fragmentTransaction.remove(streetViewFragment);        
            //View view = activity.findViewById(R.id.panelA);
            //view.setVisibility(View.VISIBLE);
            //view.invalidate();

            isStreetViewLoaded = false;

        }

    }

1 个答案:

答案 0 :(得分:0)

而不是fragmentTransaction.add()使用fragmentTransaction.replace()。因为当存在片段时,请使用replace方法替换现有片段。

  

但为什么add() - > remove() - > add()没有?

每当您致电fragmentTransaction.commit();时,它都会安排此交易的提交。提交不会立即发生,它将被安排为主线程上的工作。因此,在执行任何其他操作之前,请调用fragmentManager.executePendingTransactions();,立即执行任何此类待处理操作。

  

单个删除没有删除FrameLayout中的上一个图像(我的意思是,FrameLayout没有调用redraw())

这不是FrameLayout的问题。检查您是否传递了要删除的正确片段。