单击菜单项时,导航抽屉不会切换片段

时间:2015-06-25 20:39:45

标签: android android-fragments android-studio navigation-drawer

我只是使用Android Studio提供的默认代码来创建导航抽屉活动。

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    switch(position){
        case 0:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, new PlaceholderFragment())
                    .commit();
            Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        case 1:
            fragmentManager.beginTransaction()
                    .replace(R.id.container, new ProfileInfoFragment())
                    .commit();
            Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
    }

}

我想要做的就是在选择2个不同的项目时加载2个不同的片段。祝酒词给出正确的位置,但片段没有切换。

PlaceholderFragment.java:

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class PlaceholderFragment extends Fragment {
/**
 * The fragment argument representing the section number for this
 * fragment.
 */
private static final String ARG_SECTION_NUMBER = "section_number";

/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(
            getArguments().getInt(ARG_SECTION_NUMBER));
}
}

ProfileInfoFragment.java:

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ProfileInfoFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static ProfileInfoFragment newInstance(int sectionNumber) {
        ProfileInfoFragment fragment = new ProfileInfoFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ProfileInfoFragment() {
    }

    //Setting up the fragment
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile_info, container, false);
        return rootView;
    }
}

2 个答案:

答案 0 :(得分:2)

您忘记在每个break;后添加case。 所以发生的事情是,每次你在抽屉上选择一个项目之后,所有案例都会被选中。

所以你只看到最后一个的结果。

在这种情况下,ProfileInfoFragment

将您的代码修改为:

switch(position){
    case 0:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new PlaceholderFragment())
                .commit();
        Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        break;
    case 1:
        fragmentManager.beginTransaction()
                .replace(R.id.container, new ProfileInfoFragment())
                .commit();
        Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
        break;
}

答案 1 :(得分:0)

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

        if (id == R.id.item1) {
            fragment = new PingFragment();
            toolbar.setTitle("Fragment1");// set title
        } else if (id == R.id.item2) {
            fragment = new Fragment2();
            toolbar.setTitle("Fragment2");
        } 
        if(fragment != null) {
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.relative_layout_for_fragment, fragment)
                    .commit();
        }

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