我有两个Java类。一个是main activity,另一个是MyFragmentMyFragment包含具有映射的片段。我想显示给我的MainActivityMainActivity包含导航抽屉活动。我的两个类的代码是:

`public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.nav_loc) {

    } else if (id == R.id.nav_set) {

    } else if (id == R.id.nav_info) {

    } else if (id == R.id.nav_abt) {

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


}
`

MyFragment代码是:

public class MyFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mgoogleMap;
MapView mapView;
View view;
public MyFragment(){

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);}
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.content_main,container , false);
    return view;
}

@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mapView = view.findViewById(R.id.map);
    if(mapView!=null)
    mapView.onCreate(null);
    mapView.onResume();
    mapView.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap){
    MapsInitializer.initialize(getContext());
    mgoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(new LatLng(23.2599,77.4126)).title("BHOPAL"));
    CameraPosition bhopal = CameraPosition.builder().target(new LatLng(23.2599,77.4126)).zoom(16).bearing(0).build();
    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(bhopal));
}


}

我如何组合两个类,以便我的主要活动显示此片段。
如何使用MyFragment进行mainActivity?

最佳答案

Activity中声明一个成员字段,例如

private MyFragment fragment;


onCreate()中初始化它:

fragment = MyFragment();


然后附加并显示:

getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, "your_tag").commit()


其中container是应该用Fragment内容替换的布局的ID。

编辑:在我看来,您使用了Android Studio的“导航抽屉活动”模板。正确的布局文件应为content_main.xml(如果尚未更改该部分)。您可以在AS生成的ConstraintLayout中添加内容布局:

 <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


请注意,使用静态工厂方法进行Fragment实例化将是更好的方法,请检查this question了解更多背景信息。

10-07 19:20