在新NavigationView中单击菜单项时显示片段

时间:2015-11-30 17:12:36

标签: android fragment

我是初学者,使用Android Studio创建应用程序,当我单击导航菜单项时,我想显示不同的片段。我已经搜索了教程,但我还没有找到如何使用它。

例如,在模板中,当我点击相机时,它应显示相机片段,图库将显示图库片段,依此类推:

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

    if (id == R.id.nav_camara) {
        // Handle the camera action
        // How do I display fragment?
    } 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) {

    }

2 个答案:

答案 0 :(得分:1)

您的活动应包含FrameLayout。在onNavigationItemSelected()中,您可以为相机,图库等创建Fragment。然后将此片段放入FrameLayout

public boolean onNavigationItemSelected(MenuItem item) {
    Fragment newFragment; // This is the fragment you want to put into the FrameLayout

    int id = item.getItemId();
    if (id == R.id.nav_camara) {
        newFragment = new CameraFragment();
    } else if (id == R.id.nav_gallery) {
        newFragment = new GalleryFragment();
    } else if (id == R.id.nav_slideshow) {
        // [...]
    }

    // Let's put the new fragment into the FrameLayout
    // If you use the support action bar, use getSupportFragmentManager(), else getFragmentManager()
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment); // R.id.fragment_container = FrameLayout ID
    transaction.commit();
}

这有帮助吗?

答案 1 :(得分:0)

您可以尝试这样

final StorageReference filePath = UserProfileImagesRef.child("Profile Images").child(currentUserID + ".jpg");
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener < UploadTask.TaskSnapshot > () {
    @Override
    public void onComplete(@NonNull Task < UploadTask.TaskSnapshot > task) {
        if (task.isSuccessful()) {
            filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener < Uri > () {
                @Override
                public void onSuccess(Uri uri) {
                    String downloadUrl = uri.toString();

                    RootRef.child("Users").child(currentUserID).child("image").setValue(downloadUrl).addOnCompleteListener(new OnCompleteListener < Void > () {
                        @Override
                        public void onComplete(@NonNull Task < Void > task) {
                            if (task.isSuccessful()) {
                                loadingBar.dismiss();
                                Toast.makeText(SettingActivity.this, "Your picture Saved successfully", Toast.LENGTH_SHORT).show();

                            } else {
                                Toast.makeText(SettingActivity.this, "Problem occurred while tryng to save your picture..", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });

        } else {
            Toast.makeText(SettingActivity.this, "Your picture did NOT saved", Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }
});