2015-04-28 48 views
0

我有元素为如下包含一个复选框Listview onitemclicklistener不适用于复选框?

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

<RelativeLayout 
    android:layout_width="match_parent" 
    android:minHeight="64dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/imageViewVoucherIcon" 
     android:layout_width="50dp" 
     android:layout_centerVertical="true" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textViewVoucherText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="16sp" 
     android:layout_toLeftOf="@+id/checkboxVoucherClick" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="8dp" 
     android:textColor="@color/text" 
     android:text="sas" 
     android:layout_toRightOf="@+id/imageViewVoucherIcon" /> 

    <CheckBox 
     android:id="@+id/checkboxVoucherClick" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

<View 
    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="@color/seperator" /> 
</LinearLayout> 

这是我onitem点击收听

lvVoucher.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      CheckBox cb=(CheckBox)view.findViewById(R.id.checkboxVoucherClick); 
      if(cb.isChecked()) 
      { 
       Log.e("check","true"); 
      }else{ 
       Log.e("check","false"); 
      } 
     } 
    }); 

不过,这并不返回任何一个列表视图。

+0

的工作是它制定出来的? –

+0

你想对CheckBox和其他小部件采取不同的行动吗?或者只有你想要checkBox的点击动作 – Amsheer

回答

2

将焦点移到布局中的复选框然后它将工作。

这样的:

<CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false"/> 

,否则你必须写在适配器本身听众。

1

我有同样的问题,这为我工作;

为您的复选框,以下这些属性:

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

你有一个新的行添加到您的XML父布局 -

android:descendantFocusability="blocksDescendants" 

所以更新后的视图将像下面 -

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

它肯定会为你工作。

0

getView

holder.checkbox.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
    CheckBox cb = (CheckBox) v ; 
    Toast.makeText(getApplicationContext(), 
    "Clicked on Checkbox: " + cb.getText() + 
    " is " + cb.isChecked(), 
    Toast.LENGTH_LONG).show(); 
} 
}); 
相关问题