gpt4 book ai didi

java - 子点击时自动关闭滑动菜单

转载 作者:行者123 更新时间:2023-12-01 13:15:34 24 4
gpt4 key购买 nike

我已经正确导入了 jfeinstein 的滑动菜单库,除了一件事之外,一切正常。当我单击滑动菜单中的“net”元素时,它会打开“net” fragment ,但它不会像应该自动那样关闭菜单。

滑动菜单

 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;
}
}
<小时/>

主要 Activity

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


}
}

最佳答案

首先编写MainActivity extends SlidingFragmentActivity。在您的 MainActivity 中也进行更改

> private SlidingMenu slidingMenu 

public static SlidingMenu slidingMenu

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

> MainActivity.slideMenu.toggle()

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

关于java - 子点击时自动关闭滑动菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com