如何在Android Sliding Menu中添加片段的活动

时间:2014-09-30 03:57:27

标签: java android android-activity android-fragments

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

大家好,我已成功在我的项目中应用此Android滑动菜单。 但是,如何在不同的页面中添加一些活动?

在MainActivity中,displayView函数控制我选择的片段。你只能在"Fragment fragment = null;"中看到它,因此,CESDemo类扩展了Fragment.But我不能在CESDemo中添加我的活动,比如onTouch等等。如果我将其更改为FragmentActivity,它不允许我使用" ragmentManager.beginTransaction().replace(R.id.frame_container,fragment).commit();"

那么,我怎么能在不同的片段中应用一些活动,即使我可以设计布局但没有任何我可以创建的活动。

MainActivity.java

private void displayView(int position) {
    // update the main content by replacing fragments
    //Fragment fragment = null;
    Fragment fragment =null;
    switch (position) {
    case 0:
        fragment = new CESHome();
        break;
    case 1:
        fragment = new CESAll();
        break;
    case 2:
        fragment = new CESPending();
        break;
    case 3:
        fragment = new CESInProgress();
        break;
    case 4:
        fragment = new CESCompleted();
        break;
    case 5:
        fragment = new CESDemo();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

CESDemo.java

public class CESDemo extends Fragment {

public CESDemo(){}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_cesdemo, container, false); 
    return rootView;
}

0 个答案:

没有答案