2011-07-06 32 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