簡體   English   中英

Android ListView setOnItemClickListener無法正常工作

[英]Android ListView setOnItemClickListener not working

我正在嘗試使用TextViews創建一個ListView,我希望每個textview都能被單擊,並基於它的位置拉出一個Fragment。 但是,似乎甚至沒有調用itemOnClickListener。

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

這是列表項的xml:

<?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>

列表視圖xml文件:

<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>

這是您的整個xml抽屜(我想您嘗試做一個導航抽屜)? 如果您有父級布局,請檢查android:descendantFocusability="blocksDescendants"否則您必須將textview包裝在布局中(如果項目沒有復雜的UI,則LinearLayout就足夠了)。 new OnItemClickListenerAdapterView.OnItemClickListerner嗎?

(一個出色的入門指南http://www.vogella.com/articles/AndroidActionBar/article.html#actionbar_navigation_drawer

您使用mDrawerList.setItemsCanFocus(false);是否有原因? 您的項目布局中沒有可聚焦的項目,因此可以安全地刪除該呼叫。 之后應該可以正常工作。

還要檢查您是否在列表行項目中使用Button,它將覆蓋您的列表項目單擊事件

對於列表中的列表項,您必須在下面設置屬性

android:focusable =“ false” android:focusableInTouchMode =“ false”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM