java - Android-Studio : How to attach a map to a NavigationDrawer Fragment

标签 java android google-maps

这是我在这里的第一篇文章,真的不知道如何开始..所以如果太长请原谅.. 所以...几天前我开始在 Android-Studio 中开发一个项目(我的第一个项目..所以我对此不太了解,我仍在学习)..是关于什么的..所以它应该是有一个 map Activity +一个抽屉 Activity 。 我创建了一个 map 项目..解决了它..创建了一个位置监听器..所以到目前为止,我的项目有一个简单的 map ,其中在我当前的位置上有一个标记。 然后..我继续我的第二个目标..有一个可绘制的 Activity (右键单击项目->新建-> Activity ->抽屉导航 Activity )然后,我以覆盖的方式重写我的 list 我的 map ,所以抽屉是第一个:

我非常想将我的 map 附加到主页按钮/fragment ,这样当我按下主页按钮时,它会显示我的 map ,当我按下幻灯片时,它会显示“这是幻灯片 fragment ”

list

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.coffee">
  <!--
       The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
       Google Maps Android API v2, but you must specify either coarse or fine
       location permissions for the 'MyLocation' functionality.
  -->
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main"
          android:theme="@style/AppTheme.NoActionBar">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>

      </activity>
      <!--
           The API key for Google Maps-based APIs is defined as a string resource.
           (See the file "res/values/google_maps_api.xml").
           Note that the API key is linked to the encryption key used to sign the APK.
           You need a different API key for each encryption key, including the release key that is used to
           sign the APK for publishing.
           You can define the keys for the debug and release targets in src/debug/ and src/release/.
      -->
      <meta-data
          android:name="com.google.android.geo.API_KEY"
          android:value="@string/google_maps_key" />

      <activity
          android:name=".MapsActivity"
          android:label="@string/title_activity_maps"
          android:theme="@style/AppTheme.ActionBar">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
  </application>

</manifest> 

现在..我找不到合适的 Youtube/Google 教程来向我展示如何将 map “附加”到抽屉“主页”的第一个按钮。发现了一些内容,例如使用 MapView 或MapFragment..但没有关于如何将它们实现到按钮或要做什么/要更改的教程.. (主要 Activity )


    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

我将把链接放到 Git 上: https://github.com/Devix71/CoffeeNOU

任何帮助将不胜感激。如果我提供了太多“无用”信息,我很抱歉。

最佳答案


@Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.nav_home){
            startActivity(new Intent(MainActivity.this, MapsActivity.class));
        }

        if (id == R.id.nav_slideshow){
            //Here you can put you code
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
            transaction.addToBackStack(null);
            transaction.commit();
        }

        return true;
    }

关于java - Android-Studio : How to attach a map to a NavigationDrawer Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60578541/

相关文章:

java - Android 找不到类 'android.app.job.JobScheduler'

java - 在 Java 中通过套接字发送无符号字节

java - Activity 未找到异常 : No Activity found to handle Intent { (has extras) }

android - Chathead 的后台服务在小米 MI4I android 中不起作用

google-maps - 带有 Google Dart 的 Google Maps API key

javascript - 嵌入在 iFrame 中的 Google map API 不以纬度/经度为中心

Java:特定字符报告的数量

java - 无法对非静态类型 MyRunnable 进行静态引用

Java是此类线程安全的

javascript - 谷歌地图和油脂猴内的范围