切换并单击导航抽屉内的片段

时间:2016-03-02 00:11:37

标签: android navigation-drawer

我在Android Studio中制作了导航抽屉。

现在,抽屉里的每个片段都无处可去。

我需要知道如何在片段之间切换并点击每个片段

我知道我需要创建新的片段,但是使用什么方法,所以我可以点击导航抽屉中的设置,这样我就可以将片段分享?

这是我的代码;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) 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 = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

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

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

    @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 onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

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

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

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

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

        }

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

2 个答案:

答案 0 :(得分:0)

你的方向正确。只需在每个导航项中添加Fragment类,如下所示

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    Fragment fragment = null;
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_camera) {
        // Handle the camera action
        fragment = new DefaultFragment();
        fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();

    } else if (id == R.id.nav_gallery) {
        fragment = new ItemListFragment();
        fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();

    }
}

onCreate(Bundle savedInstanceState)方法结束时,您可以将默认片段设置为在活动加载时打开。

FragmentManager fragmentManager = getSupportFragmentManager();
    DefaultFragment fragment = new DefaultFragment();
    fragmentManager.beginTransaction().replace(R.id.first_container, fragment).commit();

答案 1 :(得分:0)

public class Contacts extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Contactos");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.miniMap);
    mapFragment.getMapAsync(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
       case android.R.id.home:
           finish();
           return true;
       default:
           return super.onOptionsItemSelected(item);
    }
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.clear();
    //Here locate and move your camera as required
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 16.0f));
    }
}