2011-07-06 31 views
4

我已經爲我的ListView實施了適配器,該適配器擴展了BaseAdapter。 我的列表項目包含每個都有OnClickListener的按鈕。當適配器包含帶onClickListener的按鈕時,OnItemClickListener無法正常工作

爲每個項目添加OnclickLister後,列表的OnItemClickListener工作。

如何解決?

代碼

以我活動 -

 ListView lv = (ListView) findViewById(R.id.list); 
    lv.setTextFilterEnabled(true); 
    lv.setItemsCanFocus(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String debString = "position = " + position + " id = " + id;     
      Log.d(TAG, debString); 
      Toast.makeText(getApplicationContext(), debString, Toast.LENGTH_SHORT).show(); 
      Contact selectedContact = dataVector.elementAt(position); 
      Bundle bundle = new Bundle(); 
      bundle.putInt(Constants.POSITION, position); 
      bundle.putString(Constants.NAME, selectedContact.getName()); 
      bundle.putString(Constants.MDN, selectedContact.getMdn()); 
      bundle.putString(Constants.STATUS, selectedContact.getStatus()); 
      String filePath = null; 
      if(contactsImagesProperties != null || !Utils.isNullOrEmpty((String) contactsImagesProperties.get(selectedContact.getMdn()))) { 
       filePath = (String) contactsImagesProperties.get(selectedContact.getMdn()); 
      } 
      bundle.putString(Constants.IMAGE, filePath); 
      Intent intent = new Intent(context, ChildDisplayActivity.class); 
      intent.putExtras(bundle); 
      getParent().startActivityForResult(intent, 10); 
     }   
在myBaseAdapter

在getView()

 bitmap = Bitmap.createScaledBitmap(bitmap, Constants.CHILD_ICON_WIDTH, Constants.CHILD_ICON_HEIGHT, false); 
    imageView.setImageBitmap(bitmap); 
    statusView.setText(Constants.StatusCodeHandler.getStatusDesc(dataVector.elementAt(position).getStatus(), context)); 
    ImageButton imageButton = (ImageButton) view.findViewById(viewIds[3]); 
    imageButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Bundle bundle = new Bundle(); 
      bundle.putInt(Constants.ACTION, Constants.CONTACT_LOCATION_CODE); 
      bundle.putString(Constants.MDN, dataVector.elementAt(position).getMdn()); 
      MainActivity.bundle = bundle; 
      TabActivity mainActivity = (TabActivity) ((UsersListActivity)context).getParent().getParent(); 
      TabHost tabHost = mainActivity.getTabHost(); 
      tabHost.setCurrentTab(Constants.MAP_TAB_INDEX); 
     } 
    }); 

在myListRaw.xml -

<ImageView android:src="@drawable/icon" 
    android:id="@+id/childListImageView" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:focusable="false" 
    android:clickable="false" 
    android:focusableInTouchMode="false" 
    android:layout_alignParentRight="true"/> 

<TextView android:id="@+id/childListTextView" 
    android:layout_marginRight="5dp" 
    android:layout_width="wrap_content" 
    android:text="TextView" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:clickable="false" 
    android:focusableInTouchMode="false" 
    android:layout_toLeftOf="@+id/childListImageView" 
    android:layout_centerVertical="true"/> 

<TextView android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:text="Child Status" 
    android:id="@+id/childListStatus" 
    android:layout_width="wrap_content"   
    android:layout_toLeftOf="@+id/childListTextView" 
    android:layout_marginRight="15dp" 
    android:focusable="false" 
    android:clickable="false" 
    android:focusableInTouchMode="false" 
    android:layout_centerVertical="true"/> 

<ImageButton android:id="@+id/childListButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Loc" 
    android:layout_marginTop="5dp"  
    android:layout_alignParentLeft="true" 
    android:focusable="false" 
    android:clickable="false" 
    android:focusableInTouchMode="false"/> 

+0

一些代碼會更好的理解........ – Sujit

+0

你可以用'lv.setItemsCanFocus(true);'試試讓你的代碼集中注意力,或者將它設置爲而不是。 – RivieraKid

+0

我改變了它對你的評論,但仍然有問題。 – eyal

回答

7

如果您將行的部分設置爲focusable(android:focusable =「true」),而不是ListItem的OnItemClickListener,則不做出響應。檢查出來

+0

沒有一個是可以聚焦的。 – eyal

+0

所以給我們一些代碼。當我們看不到你做了什麼 – Fixus

+0

eyal時,很難解決問題,除非你明確地調用了'setFocusable(false)',那麼是的,它們是可以聚焦的。您需要禁用按鈕的焦點,但是您必須管理按鈕的處理點擊。我已經在自己的應用中使用了這個功能 - 只要處理點擊操作,就可以隱式地將控件設置爲可調焦。 – RivieraKid