找到你要的答案

Q:Why OnClickListener of ListView items in Navigation Drawer never fires?

Q:为什么在导航抽屉ListView onclicklistener不火?

I'm creating an Android app with Navigation Drawer.

I faced a problem with OnItemClickListener, somehow can't get it fire at all.

All items comes to ListView correctly, but never fires any actions.

I set OnItemClickListener as Android Developer page suggests, without any luck.

MainActivity.java

Private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = (ListView)activity.findViewById(R.id.navigationList);
    setItems(listView);
}

private void setItems(ListView listView) {
    for (int i = 0; i < titles.length; i++) {
        boolean bSelected = false;
        if(i==selected) {
            bSelected = true;
        }
        adapter = new DrawerAdapter(context, generateData(items, i, titles[i], subtitles[i], subTexts[i], icons[i], bottomDivider[i], bSelected), layout);
        listView.setAdapter(adapter);
    }

    listView.setOnItemClickListener(new DrawerItemClickListener());


    listView.invalidate();
    listView.deferNotifyDataSetChanged();
}



private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // THIS NEVER FIRES!!!
        Toast.makeText(context, "Working", Toast.LENGTH_LONG).show();
    }
}

I use almost the same code on other part of my application to show messages etc, and OnItemClickListener works correctly on other places.

private ArrayList<DrawerRow> generateData(ArrayList<DrawerRow> items, int id, String title, String subtitle, String subText, int icon, boolean bottomDivider, boolean bSelected) {
    items.add(new DrawerRow(id, title, subtitle, subText, icon, bottomDivider, bSelected));
    return items;
}

What part is wrong, or do I need to do totally differently?

我创建一个Android应用程序与导航抽屉。

我面对onitemclicklistener问题,却不能得到所有的火。

所有项目涉及到ListView正确,但从来没有火灾的任何行动。

我onitemclicklistener Android开发者页面显示,没有任何的运气。

mainactivity.java

Private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = (ListView)activity.findViewById(R.id.navigationList);
    setItems(listView);
}

private void setItems(ListView listView) {
    for (int i = 0; i < titles.length; i++) {
        boolean bSelected = false;
        if(i==selected) {
            bSelected = true;
        }
        adapter = new DrawerAdapter(context, generateData(items, i, titles[i], subtitles[i], subTexts[i], icons[i], bottomDivider[i], bSelected), layout);
        listView.setAdapter(adapter);
    }

    listView.setOnItemClickListener(new DrawerItemClickListener());


    listView.invalidate();
    listView.deferNotifyDataSetChanged();
}



private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // THIS NEVER FIRES!!!
        Toast.makeText(context, "Working", Toast.LENGTH_LONG).show();
    }
}

我用几乎相同的代码在显示消息等我的应用程序的其他部分,和onitemclicklistener正常运作的其他地方。

private ArrayList<DrawerRow> generateData(ArrayList<DrawerRow> items, int id, String title, String subtitle, String subText, int icon, boolean bottomDivider, boolean bSelected) {
    items.add(new DrawerRow(id, title, subtitle, subText, icon, bottomDivider, bSelected));
    return items;
}

哪一部分是错误的,还是我需要做完全不同的?

answer1: 回答1:

Try adding android:focusable="false" on your XML for the ListView and on the root element of Drawer item layout

尝试加入Android:聚焦=“假”在你的XML的ListView和根元素上抽屉项目布局

android  android-listview  adapter  onitemclicklistener