NavigationDrawer - 是否可以通过编程方式模拟(执行)点击

时间:2014-06-17 15:00:48

标签: android

我在我的应用程序中使用导航抽屉,我想知道我是否可以按照按钮的方式单击某些导航抽屉列表项?

    slideMenuItems = new ArrayList<NavDrawerModel>();

    slideMenuItems.add(new NavDrawerModel("People"));
    slideMenuItems.add(new NavDrawerModel("Products"));

slideMenuAdapter = new NavDrawerAdapter(getApplicationContext(), R.layout.drawer_list_item, slideMenuItems);
    drawerList.setAdapter(slideMenuAdapter);

    drawerList.setOnItemClickListener(new SlideMenuClickListener());


private class SlideMenuClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Do some stuff here

    }

1 个答案:

答案 0 :(得分:1)

ListView

中致电performItemClick
public boolean performItemClick (View view, int position, long id)

Added in API level 1

Call the OnItemClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.

Parameters

view    The view within the AdapterView that was clicked.
position    The position of the view in the adapter.
id  The row id of the item that was clicked.

Returns
True if there was an assigned OnItemClickListener that was called, false otherwise is returned.

使用方法:

int position = 5;
listView.performItemClick( 
   yourAdapter.getView(position, null, null),
   position,
   yourAdapter.getItemId(position)
);

如果您使用自定义适配器,请务必正确覆盖getItemId