【问题标题】:Navigation Drawer is not switching fragments on clicking the menu items导航抽屉不会在单击菜单项时切换片段
【发布时间】:2015-06-25 20:39:44
【问题描述】:

我只是使用 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 个不同的片段。 Toast 给出了正确的位置,但片段没有切换。

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;
    }
}

【问题讨论】:

  • 您应该通过调用ProfileInfoFragment.newInstance() 而不是空的构造函数来创建片段。
  • @Lamorak 它每次只加载 ProfileInfoFragment,即使吐司说 0,PlaceholderFragment 也没有加载
  • 是的,这是一个建议,而不是解决方案。正如 Ofir 注意到的那样,您忘记了 break; 您的开关,这将解决问题。

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


【解决方案1】:

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

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

在这种情况下,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;
}

【讨论】:

  • 对不起,对不起。 :( 浪费了我生命的最后 3 个小时来解决这个问题
  • 发生在我们最好的人身上:)
【解决方案2】:
 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多