2014-03-19 32 views
0

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

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

​​
+0

使用滑动菜单的切换 –

+0

请更具体。我试着slidingMenu.toggle();在.commit()之后;但它会导致应用程序崩溃。 – Hastalafiesta

+0

如何初始化滑动菜单以及如何访问。你还没有给出你如何初始化和使用的代码。 –

回答

1

首先书写MainActivity extends SlidingFragmentActivity。也更改您的MainActivity

> private SlidingMenu slidingMenu 

public static SlidingMenu slidingMenu 

现在你MainActivity里面,你可以调用切换(),而不是this.slidingMenu.toggle(),只要你想使用它的片段里面你可以这样写:

> MainActivity.slideMenu.toggle() 

当然你也可以用同样的方法使用静态SlidingMenu的其他方法。

+0

要关闭,首先检查菜单是否显示 if(slidingMenu.isMenuShowing()) slidingMenu.toggle(); –