使用Google的导航抽屉示例更改片段

时间:2014-07-07 08:56:13

标签: android navigation-drawer

我正在使用示例导航抽屉代码来开发应用

http://developer.android.com/training/implementing-navigation/nav-drawer.html

我想将片段更改为listview或map ... etc

但样本使用图片

我想知道如何更改新片段而不是显示图像?

这是onCreateView函数代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
    int i = getArguments().getInt(ARG_PLANET_NUMBER);
    String planet = getResources().getStringArray(R.array.planets_array)[i];

    int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                    "drawable", getActivity().getPackageName());
    ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
    getActivity().setTitle(planet);
    return rootView;
}

1 个答案:

答案 0 :(得分:0)

查看图片本身是否在PlanetFragment中加载。因此,根据您的逻辑,您可以设置附加导航抽屉列表项位置的任何片段。它不一定要在单个片段中显示任何内容。
方法中private void selectItem(int position) {}具有参数位置,因此使用int加载不同的片段。

 Fragment fragment;
 if(position==0)
 {
   fragment = new FirstFragment();
 }else if(position==1)
 {
   fragment = new SecondFragment();
 }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, fragment).commit();