我是这方面的新手,我想知道我是否可以让我的 Activity 扩展两个java类
为什么我需要它?
- 在我的 Activity 中,我需要一个 map 和一个抽屉导航,它将提供用户可以使用的所有选项。
这是我的 MainActivity 中的代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
setTitle("");
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id) {
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
- 我还发现一项 Activity 无法延长两个类。任何其他解决方案我怎样才能实现它。 提前致谢。
最佳答案
在 Java 中,无法从两个类进行扩展 但是您可以通过从该类创建一个新对象并在程序中使用它来获取该类的所有功能。
You can also look at this link, it may help you
更新:
我使用此方法在没有扩展类的程序中使用 Google Maps API。
public class MapInfoFragment extends Fragment implements OnMapReadyCallback {
private static GoogleMap mMap;
public MapInfoFragment() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_travelling_info, container, false);
SupportMapFragment mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.frmap);
lnl_map_fragment = rootView.findViewById(R.id.lnl_map_fragment);
mMapFragment.getMapAsync(this);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
googleMap.setMapType(1);
googleMap.getUiSettings().setMapToolbarEnabled(false);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.setTrafficEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setAllGesturesEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(false);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
origin_marker_option.position(origin_ltln)
.title("Origin")
.icon(BitmapDescriptorFactory.fromBitmap(btm_origin))
.flat(false);
googleMap.addMarker(origin_marker_option);
if (ar_dest_lat_lon.size() > 0) {
dest_marker_option_1.position(ar_dest_lat_lon.get(0))
.title("Dest1")
.icon(BitmapDescriptorFactory.fromBitmap(btm_dest_1))
.flat(false);
googleMap.addMarker(dest_marker_option_1);
} else {
builder.include(new LatLng(origin_ltln.latitude - 0.003, origin_ltln.longitude - 0.003));
builder.include(new LatLng(origin_ltln.latitude + 0.003, origin_ltln.longitude + 0.003));
}
builder.include(origin_ltln);
for (LatLng dest_lat_lon : ar_dest_lat_lon)
builder.include(dest_lat_lon);
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels / 3;
int padding = (int) (width * 0.12); // offset from edges of the map 12% of screen
int w = lnl_map_fragment.getWidth();
int h = lnl_map_fragment.getHeight();
int p = (int) (w * 0.12);
try {
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
} catch (Exception e) {
e.printStackTrace();
}
initMapCameraPosition();
}
private void initMapCameraPosition() {
if (mMap == null) return;
}
}
- 应尊敬的提问者的要求,放置了代码
关于java - 如何在一项 Activity 上扩展两个类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131785/