onItemClickListener不起作用解決辦法(原創)


  最近寫了一個項目,界面使用的是幀布局,里面放置了listview顯示聯系人,以及右側有對聯系人的字母索引定位。

結果在對聯系人listview設置onItemClickListener時,發現竟然不起作用。

  下面的是布局文件以及設置代碼

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

> <ListView android:id="@+id/contact_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:focusable="true" android:focusableInTouchMode="true" android:dividerHeight="1px" android:scrollbars="none" /> <LinearLayout android:layout_width="28dp" android:layout_height="match_parent" android:layout_gravity="right|center" android:layout_marginBottom="6dp" android:layout_marginTop="10dp" android:gravity="right" android:orientation="vertical" > <ListView android:id="@+id/contact_letter" android:layout_width="28dp" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:focusable="true" android:focusableInTouchMode="true" android:scrollbars="none" /> </LinearLayout> </FrameLayout>

    在activity中設置onItemCllickListener,
代碼如下:

  lv_contact.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
                Log.i("my", "onItemClick clicked");
                
            }
            
        });

  item的布局如下:

<ImageView 
android:scaleType="centerInside"
android:layout_marginLeft="12dp"
android:layout_marginTop="5dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_contact_picture"
android:id="@+id/contact_contactinfoitem_iv_photo"
/> 
  
<TextView  
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_name"
android:text="xxx"
/> 
<TextView  
android:singleLine="true"
android:ellipsize="marquee"
android:textStyle="bold"
android:marqueeRepeatLimit="marquee_forever"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:id="@+id/contact_contactinfoitem_tv_phone"
android:text="xxx"
/>

 

     

 

當點擊聯系人listiew的時候,沒有打出對應的log,非常奇怪,開始的時候還以為是設置了onTouchListener的原因,因為onTouchListener返回結果會影響是否需要繼續處理屏幕事件。
   檢查發現onTouchListener里面,返回結果是false,不是true,這意味着屏幕事件是繼續傳遞處理的,不會影響到onItemClickListener的處理。

     去網上查了下發現有說  xml中有個焦點屬性會影響onTouchListener,需要將其改為false

     再次檢測xml文件,里面確實設置有這兩個屬性

android:focusable="true"
android:focusableInTouchMode="true"

這兩個屬性的看名字就知道到意思,
android:focusable="true"-------
       設置是否獲得焦點。若有requestFocus()被調用時,后者優先處理。注意在表單中想設置某一個如EditText獲取焦點,光設置這個是不行的,需要將這個EditText前面的focusable都設置為false才行。在Touch模式下獲取焦點需要設置focusableInTouchMode為true

android:focusableInTouchMode="true"----

     設置在Touch模式下View是否能取得焦點。

     在xml中修改,這屬性為false,運行工程,發現還是一樣的onItemCllickListener不起作用,這就糾結了。

  因為趕時間,干脆在adapter中,寫item的onclicklistener算了。代碼如下:

  

contact_fl.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View view)
            {
                Log.i("my", "onClick clicked");
                
            }
            
        });

 

       運行項目,item的點擊效果有了。但是同時有新的問題出現了。在我的ontouchListener種,onkeydown事件消失了。。。。

只有onkeymove和onkeyup。

    經過分析得出結論,那就是肯定有方法或者屬性,設置的時候沖突了,item的布局文件中沒有button之類的空間。重點查看是否有方法或者屬性使得click事件消失了

     再次到adapter中仔細檢查,發現有個兩個這樣的方法:

@Override
    public boolean areAllItemsEnabled()
    {
        // all items are separator
        return false;
    }

    @Override
    public boolean isEnabled(int position)
    {
        // all items are separator
        return false;
    }

    查看說明:

      Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item). The result is unspecified if position is invalid. An ArrayIndexOutOfBoundsException should be thrown in that case for fast failure.

    意思就是說,如果當前指定的位置不是一個separator--分隔符(分隔符是一個不能選中,不能點擊的item),那么返回true。

    那么趕緊改為true,運行項目,效果都有了。

  成功解決。

  

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM