我最近完成了一个项目,使用活动和导航抽屉,工作得很好。我决定使用更强大的项目,我会使用片段而不是新活动。当然,我的代码几乎相同,但是当选择导航抽屉中的项目时,我无法触发onClick。任何帮助都会很精彩。我关心的是drawerNavListView(左侧导航抽屉)
MainTemplate扩展了活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_template);
// drawer creation
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListView = (ListView) findViewById(R.id.right_drawer);
drawerNavListView = (ListView) findViewById(R.id.left_drawer);
drawerItems = new ArrayList<NavDrawerItem>();
navItem = new ArrayList<NavMenuItem>();
navItem.add(new NavMenuItem("1", false, null));
navItem.add(new NavMenuItem("2", true, "0"));
navItem.add(new NavMenuItem("3", false, null));
navItem.add(new NavMenuItem("4", false, null));
navItem.add(new NavMenuItem("5", false, null));
navItem.add(new NavMenuItem("6", false, null));
navItem.add(new NavMenuItem("7", false, null));
menuAdapter = new NavMenuAdapter(getApplicationContext(), navItem);
drawerNavListView.setAdapter(menuAdapter);
// create the blank fragment
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MainPageFrag mFrag = new MainPageFrag();
ft.add(R.id.fragment_space, mFrag);
ft.commit();
// JSON get class creation for notifications
new myNotificationTask().execute(); // call class at bottom
...
// listener for clicking of a notification drawer item
drawerListView.setOnItemClickListener(new SlideOptionsClickListener());
// listener for selecting a navigation drawer item
drawerNavListView.setOnItemClickListener(new SlideNavClickListener());
// getting screen size
Display display = getWindowManager().getDefaultDisplay();
screenSize = new Point();
display.getSize(screenSize);
// setting pixels per minute for device
ppm = (screenSize.x * 4) / (25 * 60);
// create handler to call the notification update every 30 seconds
final Handler mHandler = new Handler();
...
}
...(同一类)
private class SlideNavClickListener implements ListView.OnItemClickListener {
// this is where we inflate the fragment to the screen depending on the
// selection
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
switch (position) {
case 0:
ScheduleViewFrag sFrag = new ScheduleViewFrag();
ft.replace(R.id.fragment_space, sFrag);
ft.addToBackStack(null);
ft.commit();
break;
case 1:
DelayFrag dFrag = new DelayFrag();
Bundle bundle = new Bundle();
bundle.putString(NOTIF_ID, drawerItems.get(position).getId());
dFrag.setArguments(bundle);
ft.replace(R.id.fragment_space, dFrag);
ft.addToBackStack(null);
ft.commit();
break;
default: // add more fragments once implemented here
break;
}
drawerNavListView.setSelection(position);
mDrawer.closeDrawer(drawerNavListView);
}
}
答案 0 :(得分:2)
原来整个问题都在我的主要版面的xml文件中。显然,让包含主屏幕片段的线性布局在上面,导航抽屉的两个列表视图激活了onclick听众。我原本在两者之下,并且出于某种原因,这引起了我的问题。希望这有助于将来的某个人。