Question

I have imported correctly the sliding menu library by jfeinstein and all is working except one thing. When I click on the "net" element in the sliding menu it opens the "net" fragment but it doesn't close the menu as it should does automatically.

SlidingMenu

 public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {
private SlidingMenu slidingMenu ;
private ExpandableListView sectionListView;

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

    List<Section> sectionList = createMenu();

    View view = inflater.inflate(R.layout.slidingmenu_fragment, container, false);
    this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);
    this.sectionListView.setGroupIndicator(null);
    SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
    this.sectionListView.setAdapter(sectionListAdapter); 

    this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
          @Override
          public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            return true;
          }
        });

    this.sectionListView.setOnChildClickListener(this);

    int count = sectionListAdapter.getGroupCount();
    for (int position = 0; position < count; position++) {
        this.sectionListView.expandGroup(position);
    }

    return view;
}

private List<Section> createMenu() {
    List<Section> sectionList = new ArrayList<Section>();

    Section oDemoSection = new Section("Demos");
    oDemoSection.addSectionItem(101,"Test", "lol");
    oDemoSection.addSectionItem(102, "Airport (AsyncTask)", "lol");

    Section oGeneralSection = new Section("General");
    oGeneralSection.addSectionItem(201, "Settings", "lol");
    oGeneralSection.addSectionItem(202, "Rate this app", "lol");
    oGeneralSection.addSectionItem(203, "Eula", "lol");
    oGeneralSection.addSectionItem(204, "Quit", "lol");

    sectionList.add(oDemoSection);
    sectionList.add(oGeneralSection);
    return sectionList;
}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    Fragment fragment=null;

    switch ((int)id) {

    case 101:
        fragment = new Net();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        break;
    case 102:
        //TODO
        break;
    case 201:
        //TODO
        break;
    case 202:
        //TODO
        break;
    case 203:
        //TODO
        break;
    case 204:
        //TODO
        break;
    }

    return false;
}
}

MainActivity

 public class MainActivity extends Activity {
private SlidingMenu slidingMenu ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    slidingMenu = new SlidingMenu(this);
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
    slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
    slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    slidingMenu.setFadeDegree(0.35f);
    slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    slidingMenu.setMenu(R.layout.slidingmenu);

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public void onBackPressed() {
    if ( slidingMenu.isMenuShowing()) {
        slidingMenu.toggle();
    }
    else {
        super.onBackPressed();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ( keyCode == KeyEvent.KEYCODE_MENU ) {
        slidingMenu.toggle();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        this.slidingMenu.toggle();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }


        }
}   
Was it helpful?

Solution

First of all write MainActivity extends SlidingFragmentActivity. Change also in your MainActivity

> private SlidingMenu slidingMenu 

with

public static SlidingMenu slidingMenu

now inside your MainActivity you can call toggle() instead of this.slidingMenu.toggle() and whenever you want to use it inside a Fragment you can write:

> MainActivity.slideMenu.toggle()

of course you can use every other method of your static SlidingMenu by the same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top