想要从导航抽屉移至另一个活动

时间:2018-10-26 04:42:17

标签: android

我有一个地图活动。它有一个导航抽屉。但是,当我单击任何导航项时,我无法进行其他活动。我在这里附加我的代码。没有错误。当我单击导航抽屉中的任何一项时,它不会移动到其他活动。

     nv = (NavigationView) findViewById(R.id.nv);
            nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;
                        case R.id.dona:
                            Intent j = new Intent(HomeActivity.this, DonateActivity.class);
                            startActivity(j);
                            break;

                        case R.id.pla:
                            Intent k = new Intent(HomeActivity.this, PlantActivity.class);
                            startActivity(k);
                            break;

                        case R.id.not:
                            Intent l = new Intent(HomeActivity.this, NotificationActivity.class);
                            startActivity(l);
                            break;

                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                    return false;

                }
            });





 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }

    }






 protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (t.onOptionsItemSelected(item))
            return true;
        int id = item.getItemId();



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


            return super.onOptionsItemSelected(item);


    }
}

4 个答案:

答案 0 :(得分:0)

  

您需要return true末尾的onNavigationItemSelected

  nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.profile:

                            Intent i = new Intent(HomeActivity.this, ProfileActivity.class);
                            startActivity(i);
                            break;

                        /*--your code--*/


                        case R.id.hist:
                            Intent m = new Intent(HomeActivity.this, HistoryActivity.class);
                            startActivity(m);
                            break;

                        case R.id.hel:
                            Intent n = new Intent(HomeActivity.this, HelpActivity.class);
                            startActivity(n);
                            break;



                    }

                yourDrawer.closeDrawer(GravityCompat.START);

                    return true;       //you need to return true here, not false

                }
            });

答案 1 :(得分:0)

尝试

  • 我正在使用像这样的直接选中项目的点击方法

    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    
    if (id == R.id.nav_buildings) {
        startActivity(new Intent(SuperAdminHomeActivity.this,BuildingsActivity.class));
    
    } else if (id == R.id.nav_companies) {
        startActivity(new Intent(SuperAdminHomeActivity.this,CompaniesActivity.class));
    
    } else if (id == R.id.nav_reports) {
        startActivity(new Intent(SuperAdminHomeActivity.this,ReportsActivity.class));
    
    } else if (id == R.id.nav_invoice) {
        startActivity(new Intent(SuperAdminHomeActivity.this,InvoiceActivity.class));
    
    } else if (id == R.id.nav_notifications) {
        startActivity(new Intent(SuperAdminHomeActivity.this,NotificationsActivity.class));
    
    } else if (id == R.id.nav_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketActivity.class));
    
    }
    else if (id == R.id.nav_my_ticket) {
        startActivity(new Intent(SuperAdminHomeActivity.this,TicketsAddActivity.class));
    }
    
    else if(id==R.id.nav_logout){
        alertDialogue("Do you want to logout?");
    }
    
    drawer.closeDrawer(GravityCompat.START);
    return true;
     }
    
  • 我没有使用抽屉式点击侦听器。

  • 这是我的抽屉布局

enter image description here

工作正常,请尝试

答案 2 :(得分:0)

仔细检查了您的完整代码后,我意识到没有调用.setNavigationItemSelectedListener。即使抽屉正在显示,它也位于活动的后面。因此,您实际上并没有单击它。

所以简单的解决方法是:

nv.bringToFront();

在初始化nv之后将其放入

答案 3 :(得分:-1)

使您的导航视图成为全局变量