儿童点击自动关闭滑动菜单

时间:2014-03-19 09:05:53

标签: java android slidingmenu jfeinstein

我已经正确导入了jfeinstein的滑动菜单库,除了一件事之外,所有工作都正常。当我点击滑动菜单中的“net”元素时,它会打开“net”片段,但它不会自动关闭菜单。

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


        }
}   

1 个答案:

答案 0 :(得分:1)

首先写MainActivity extends SlidingFragmentActivity。也可以在MainActivity中更改

> private SlidingMenu slidingMenu 

public static SlidingMenu slidingMenu

现在在你的MainActivity中你可以调用toggle()而不是this.slidingMenu.toggle(),每当你想在Fragment中使用它时你可以写:

> MainActivity.slideMenu.toggle()

当然,您可以通过相同的方式使用静态SlidingMenu的所有其他方法。