简体   繁体   中英

Android ListView setOnItemClickListener not working

I am trying to make a ListView with TextViews, I want each textview to be able to be clicked and based on it's position a Fragment is pulled up. However, the itemOnClickListener seems to not even be called.

        mDrawerList = (ListView) findViewById(R.id.devices_drawer);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, drawerInfo));
    mDrawerList.setItemsCanFocus(false);
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                final int pos, long id) {
            Log.d(TAG, "CLICKED " + pos);
            selectDevice(pos);
        }
    });

here's the xml for the list item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/deviceText"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

List view xml file:

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/devices_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="#111"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.v4.widget.DrawerLayout>

It's your whole xml drawer (I presume you try to do a navigation drawer) ? If you have a parent layout check the android:descendantFocusability="blocksDescendants" otherwise you have to wrap your textview in a layout (LinearLayout is sufficient if you don't have a complex UI for the item). The new OnItemClickListener is an AdapterView.OnItemClickListerner ?

(An excellent how-to http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_navigation_drawer )

Is there a reason you are using mDrawerList.setItemsCanFocus(false); ? You don't have any focusable items in your item layout, so it's safe to remove that call. Should work ok after that.

还要检查您是否在列表行项目中使用Button,它将覆盖您的列表项目单击事件

For list items inside your list.. you have to set the attributes below

android:focusable="false" android:focusableInTouchMode="false"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM