Search code examples
androidandroid-fragmentsactionbarsherlock

SherlockNavigationDrawer (implements actionbarsherlock)


i am trying to modify the example from SherlockNavigationDrawer. It has only one fragment but i have no idea about to add more fragments each one with his own layout, not the same for all...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CONTENT_VIEW_ID);
    setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    if (savedInstanceState == null) {
        setInitialFragment();
    }
}


private void setInitialFragment() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(CONTENT_VIEW_ID, MainFragment.newInstance()).commit();
}

But my idea is to call a new fragment when clicking an item from the listview which is inside the MainFragment

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mContent.setText(Shakespeare.DIALOGUE[position]);
        mActionBar.setTitle(Shakespeare.TITLES[position]);
        mDrawerLayout.closeDrawer(listView);

       //START A NEW FRAGMENT HERE DEPENDING OF THE POSITION CLICKED
    }
}

Any idea for it? Thanks in advance


Solution

  • Inside the sherlockFragment, ie, the only fragment that comes with the example.

    public class MainFragment extends SherlockFragment {
      Customfragment1 fragment1;
      Customfragment2fragment2;
      @Override
      public void onCreate(Bundle savedInstanceState) {
            fragment1 = new ContactsFragment();
        fragment2 = new GroupsFragment();
      }
    
          private void selectItem(int position) {
    
                FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
                // Locate Position
                switch (position) {
                case 0:
                    ft.replace(R.id.content_frame, fragment1, "Customfragment1");
                    ft.addToBackStack("Customfragment1");
                    break;
                case 1:
                    ft.replace(R.id.content_frame, fragment2, "Customfragment2");
                    ft.addToBackStack("Customfragment2");
                    break;
                }
                ft.commit();
                mDrawerLayout.closeDrawer(listView);
            }
    }