2012-03-28 31 views
11

Tôi có một bố cục mục như thế này, và thiết lập nền bằng cách sử dụng mục chọnOnItemClickListener không hoạt động với hộp kiểm?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/itemselector" 
android:orientation="horizontal" > 
<CheckBox 
    android:id="@+id/message_row_checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/message_row_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" 
     android:textColor="@color/black" /> 

itemselector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
android:state_pressed="true" 
android:drawable="@color/yellow" /> 
<item 
android:state_selected="true" 
android:drawable="@color/green" /> 
<item 
android:drawable="@color/white" /> 
</selector> 

Tôi có một ListView mà sẽ content một số mặt hàng. Sau đó, tôi đã sử dụng setOnItemClickListener() nhưng nó không hoạt động. Tôi thấy rằng nếu tôi loại bỏ các hộp kiểm trong mục, mọi thứ sẽ là okey.

Sự cố giữa hộp kiểm và người nghe ở đây là gì? Bạn có thể cho tôi một số giải pháp không?

Cập nhật: Đây là mã của người nghe

mainListView.setAdapter(messageAdapter); 
mainListView.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
         Message p = (Message) arg0.getItemAtPosition(arg2); 
         Toast.makeText(TarsiusActivity.this, p.getTitle(), Toast.LENGTH_LONG); 
         Log.i("Item Clicked", p.getTitle()); 
        } 
}); 

ps: Tôi muốn làm cho hộp thư như gmail trên android. Mỗi hàng có một hộp kiểm và người dùng có thể bấm vào mục nếu họ muốn nhìn thấy thông báo

+1

Vui lòng hiển thị mã bạn đã sử dụng để đặt 'onClickListener()'. – PearsonArtPhoto

+0

Tôi đã cập nhật mã – TrungNguyen

+0

bản sao có thể có của [ListView OnItemClickListener Not Responding?] (Http://stackoverflow.com/questions/2367936/listview-onitemclicklistener-not-responding) –

Trả lời

16

Các onItemClickListener cho ListView sẽ không hoạt động nếu bạn có bất kỳ clickables như nút, ImageButton, Checkbox, v.v. trong listView. Thêm

mainListView.setItemsCanFocus(true); 

Tham khảo ListView OnItemClickListener Not Responding?

+0

cảm ơn bạn đã tham khảo;) Tôi sẽ thử rằng – TrungNguyen

+4

Không hoạt động nếu hộp kiểm có mặt trong nút danh sách – TacB0sS

1

Sử dụng setOnCheckedChangeListener thay vì onItemClickListne r cho hộp kiểm

CheckBox check; 
check=new CheckBox(this); 
check.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // TODO Auto-generated method stub 
    } 
}); 
+0

có giải pháp nào khác không? Tôi muốn làm cho nó trông giống như gmail trên Android. Vẫn có hộp kiểm và nếu người dùng nhấp vào mục nếu họ muốn đọc tin nhắn – TrungNguyen

1

Bạn có thể thêm mã này trong vòng OnItemClickListener phương pháp của bạn:

public void onItemClick(AdapterView parent, View view, int position, long id){ 
    CheckBox box = (CheckBox)view.findViewById(R.id.course_search_checkbox); 
    box.setChecked(true); 
} 
+0

cảm ơn giải pháp của bạn nhưng bạn đã sử dụng GMAIL trên Android? :) – TrungNguyen

+0

vâng tôi sử dụng Gmail trong android .. –

+1

yup ... trong hộp thư đến, nếu bạn nhấp vào hộp kiểm, sẽ có một số tùy chọn cho bạn NHƯNG nếu bạn nhấp vào phần còn lại, nó sẽ mở thư.Điều gì là những gì tôi muốn;) – TrungNguyen

36

Cách tốt nhất để làm điều này là thiết lập các thuộc tính sau cho hộp kiểm của bạn:

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

Tôi gặp vấn đề tương tự và đã thực hiện việc này.

+1

Làm việc nếu bạn vô hiệu hóa hộp kiểm! – TacB0sS

+1

Làm việc cho tôi, Cảm ơn –

+0

Làm việc cho tôi. Cảm ơn – Guilorgsorb

8

Chỉ cần thêm

android:descendantFocusability="blocksDescendants"

Để bạn LinearLayout cấp cao nhất của listitem.