2016-09-07 2 views
1

이 예에서는 Google지도를 구현했습니다. 이 응용 프로그램에는 탐색 서랍도 있습니다. 탐색 창에는 앱의 메뉴 역할을하는 목록보기가 있습니다. 메뉴 항목은 "map"과 "Fragment 1"입니다. 탐색 창에서 '지도'를 선택하면 화면이 2 초 동안 정지 한 후 탐색 서랍이 뒤로 밀려지도가로드됩니다. 이것은 사용자 경험에 좋지 않습니다. 이 상황에서 앱이 정지되는 것을 방지 할 수있는 방법이 있습니까? 탐색 창을 정상적으로 작동시키고 사용자의지도가 아직로드 중임을 나타내는 방법?Google지도를로드하는 동안 얼어 붙는 것을 방지하는 방법

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"빈 조각입니다

MainActivity.java : 여기에 코드입니다.

나는 이렇게 했어.하지만 같은 결과. 대신 당신은 아마 직접지도보기 클래스를 사용해야합니다 SupportMapFragment를 사용

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

. 불행하게도 Google이 맵 "asynch"의로드를 호출하더라도 실제로는 UI 스레드에서 실행되며 동결의 원인은 무엇입니까. 나에게 도움이 무엇

는 스레드 배경에서 응용 프로그램을 시작하는 처음이 작업을 실행하는 것입니다 그것은 기본적으로지도보기를 미리로드 :

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

같은 결과가 작동하지 않았습니다. 나는 getMapAsync (this)를 호출 할 수 없다. 스레드에. 그것은 주 스레드에서 호출되어야합니다. –

관련 문제