2016-09-07 69 views
1

在這個例子中,我已經在一個片段中實現了谷歌地圖。這個應用程序也有一個導航抽屜。在導航抽屜中有一個列表視圖,它充當應用程序的菜單。菜單項是「地圖」和「碎片1」。當我從導航抽屜中選擇「地圖」時,屏幕會凍結兩秒鐘,然後導航抽屜滑回並加載地圖。這對用戶體驗不利。在這種情況下,有什麼方法可以防止應用程序凍結?一種允許導航抽屜正常工作並指示用戶地圖的方式仍在加載?下面是代碼:如何防止等待加載谷歌地圖時凍結

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    private DrawerLayout drawerLayout; 
    private FrameLayout framlaout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.initComponents(); 
    } 

    public void initComponents(){ 
     toolbar = (Toolbar) findViewById(R.id.toolBar); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     framlaout = (FrameLayout) findViewById(R.id.container); 

     toolbar.setTitle("example"); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     NavigationFragment navigationFragment = (NavigationFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.navigation_fragment); 
     navigationFragment.setupNavigationProperty(R.id.navigation_fragment,drawerLayout,toolbar); 
     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.container,new BlankFragment()).commit(); 

    } 

MapFragment.java

public class MapFragment extends Fragment implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    public MapFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_map, container, false); 
     SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     return view; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

NavigationFragment.java

public class NavigationFragment extends Fragment { 


    private ListView listView; 
    private ArrayList<String> menuItems; 
    private View containerView; 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle drawerToggle; 

    public NavigationFragment() { 
     // Required empty public constructor 
    } 


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

     View view = inflater.inflate(R.layout.fragment_navigation, container, false); 

     initComponents(view); 
     onClickItemnListnener(); 

     return view; 
    } 

    public void initComponents(View view){ 
     menuItems = new ArrayList<>(); 
     menuItems.add("Fragment 1"); 
     menuItems.add("map"); 

     listView = (ListView) view.findViewById(R.id.menu); 
     listView.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,menuItems)); 
    } 

    public void setupNavigationProperty(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar){ 
     containerView = getActivity().findViewById(fragmentId); 
     this.drawerLayout = drawerLayout; 
     drawerToggle = new ActionBarDrawerToggle(getActivity(),drawerLayout,toolbar 
       ,R.string.drawer_open,R.string.drawer_close); 
     drawerLayout.addDrawerListener(drawerToggle); 
     drawerLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       drawerToggle.syncState(); 
      } 
     }); 
    } 

    public void onClickItemnListnener(){ 
     final FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       switch (menuItems.get(position)){ 
        case "Fragment 1": 
         fragmentManager.beginTransaction().replace(R.id.container,new BlankFragment()).commit(); 
         closeNavigationDrawer(drawerLayout,containerView); 
         break; 
        case "map": 
         fragmentManager.beginTransaction().replace(R.id.container,new MapFragment()).commit(); 
         closeNavigationDrawer(drawerLayout,containerView); 
         break; 
       } 
      } 
     }); 
    } 

    public void closeNavigationDrawer(DrawerLayout layout,View fragmentView){ 
     drawerLayout = layout; 
     drawerLayout.closeDrawer(containerView); 
    } 

「片段1」 是一個空的片段。

我做了這樣的事情但結果相同。

final SupportMapFragment[] mapFragment = new SupportMapFragment[1]; 

     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       mapFragment[0] = (SupportMapFragment) getChildFragmentManager() 
         .findFragmentById(R.id.map); 
      } 
     }; 
     try { 
      Thread thread = new Thread(runnable); 
      thread.start(); 
      thread.join(); 
      mapFragment[0].getMapAsync(MapFragment.this); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

回答

-2
public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    private DrawerLayout drawerLayout; 
    private FrameLayout framlaout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     this.initComponents(); 
    } 

    public void initComponents(){ 
     toolbar = (Toolbar) findViewById(R.id.toolBar); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     framlaout = (FrameLayout) findViewById(R.id.container); 

     toolbar.setTitle("example"); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     NavigationFragment navigationFragment = (NavigationFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.navigation_fragment); 
     navigationFragment.setupNavigationProperty(R.id.navigation_fragment,drawerLayout,toolbar); 
     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.container,new BlankFragment()).commit(); 

} 

MapFragment.java

+0

您還沒有改變任何東西嗎? –

-1

而不是使用SupportMapFragment你應該直接使用MapView類的。不幸的是,即使Google調用地圖加載「異步」,它實際上在UI線程中運行,這就是導致凍結的原因。

什麼幫助我是在後臺運行該線程的應用程序第一次啓動時,它基本上是預裝的MapView:

Runnable initMap = new Runnable() { 
     @Override 
     public void run() { 
      MapView mapView = new MapView(YourActivity.this); 
      mapView.onCreate(null); 
     } 
    }; 
    new Thread(initMap).start(); 
+0

,沒有工作相同的結果。我無法調用getMapAsync(this);在線程上。它必須在主線程中調用。 –