有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:无法集成抽屉导航,因为已经在为Google地图调用extends

我正在做一个Android项目,我想在其中添加抽屉功能,我已经为它准备好了课程

问题是抽屉代码通过extends或扩展想要添加抽屉的类来工作,类似地,我的GoogleMaps代码也以同样的方式工作。但是由于多重继承,我不能扩展两个类

我该怎么办

谷歌地图代码:

public class MapsActivity extends FragmentActivity {

    GoogleMap googleMap;
    SharedPreferences sharedPreferences;
    int locationCount = 0;

    private RestaurantServiceImpl restaurantService = new RestaurantServiceImpl();

    List<RestRestaurant> restRestaurantList = new ArrayList<>();

    GPSTracker gps;

    double longitude, latitude;



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

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Opening the sharedPreferences object
        sharedPreferences = getSharedPreferences("location", 0);

        // Getting number of locations already stored
        locationCount = sharedPreferences.getInt("locationCount", 0);


        // Getting stored zoom level if exists else return 0
        String zoom = sharedPreferences.getString("zoom", "12");

        gps = new GPSTracker(MapsActivity.this);

        if (gps.canGetLocation()) {
            longitude = gps.getLongitude();
            latitude = gps.getLatitude();
            restRestaurantList = this.restaurantService.getRestaurantsByLocation(longitude,latitude);

            double lat=0, longi=0;
            for(RestRestaurant restRestaurant : restRestaurantList){
                drawMarker(new LatLng(restRestaurant.getLatitude(),restRestaurant.getLongitude()), restRestaurant.getRestaurantName(), restRestaurant.getRestaurantId());
                lat = restRestaurant.getLatitude();
                longi = restRestaurant.getLongitude();
            }

            googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,longi)));

            // Setting the zoom level in the map on last position  is clicked
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));

        } else {
            gps.showSettingsAlert();
        }

        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                int restoId = Integer.valueOf(marker.getSnippet());
                Intent intent = new Intent(MapsActivity.this, MenuCardList.class);
                intent.putExtra("restaurantid", restoId);
                startActivity(intent);
                finish();
                return true;
            }
        });
    }

我需要扩展这个类来添加抽屉:

public class DrawerLoader extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;



    private ArrayList<DrawerModel> navDrawerItems;
    private DrawerListAdapter adapter;

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


    }
    public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
        mTitle = mDrawerTitle = getTitle();
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        navDrawerItems = new ArrayList<DrawerModel>();

如果需要更多信息,请告诉我


共 (1) 个答案

  1. # 1 楼答案

    忽略所有关于导航抽屉的工作,这将是我的首选解决方案,因为不需要额外的片段、列表填充和新类

    1.创建一个nav_menu.xml(在菜单文件夹中):

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <group
            android:checkableBehavior="single">
    
            <item
                android:id="@+id/drawer_home"
                android:checked="true"
                android:icon="@drawable/ic_home_black_24dp"
                android:title="Home"/>
    
            <item
                android:id="@+id/drawer_favourite"
                android:icon="@drawable/ic_favorite_black_24dp"
                android:title="Favourites"/>
    
            <item
                android:id="@+id/drawer_settings"
                android:icon="@drawable/ic_settings_black_24dp"
                android:title="Settings"/>
    
    </group>
    

    2.用DrawerLayout包装活动布局:

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <!  Your activity layout here >
    
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/nav_menu"/>
    
    </android.support.v4.widget.DrawerLayout>
    

    3、更新你的活动java代码:

    //If you have a toolbar (recommended!)
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final ActionBar actionBar = getSupportActionBar();
    
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true); 
    }
    
    //and finally the Navigation View Code:
    final NavigationView navigation = (NavigationView) findViewById(R.id.navigation);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                navigation.setCheckedItem(menuItem.getItemId());
                drawerLayout.closeDrawers();
    
                switch (menuItem.getItemId()) {
                    case R.id.drawer_home:
                        //open fragment home
                        return true;
                    case R.id.drawer_favourite:
                        //open fragment favourite
                        return true;
                    case R.id.nav_drawer_channels:
                        showFragment(2);
                        return true;
                    case R.id.drawer_settings:
                        //open settings
                        return true;
                }
                return false;
            }
        });
    
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
                this,
                drawerLayout,
                toolbar,
                R.string.drawer_open, //simply a String "open"
                R.string.drawer_close) {  //simply a String "close"
            public void onDrawerClosed(View view) {
                super.onDrawerOpened(drawerLayout);
                drawerToggle.syncState();
            }
    
            public void onDrawerOpened(View drawerView) {
                super.onDrawerClosed(drawerLayout);
                drawerToggle.syncState();
            }
        };
    
        drawerLayout.setDrawerListener(drawerToggle);
        drawerToggle.syncState();  
    
        //And, last but not least, open the drawer with a Click on 'home'
       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
           switch (item.getItemId()) {
               case android.R.id.home:
                   drawerLayout.openDrawer(GravityCompat.START);
               return true;
    }
    
    return super.onOptionsItemSelected(item);
    }
    

    你完了!没有多余的片段,不必要的和混乱的代码