5

Tôi muốn các mục trong RecyclerView có phản hồi chạm hoặc gợn sóng khi được nhấn, nhưng chúng dường như không hoạt động và tôi nghĩ đó là do hộp kiểm.RecyclerView mục không hiển thị gợn sóng/phản hồi cảm ứng khi mục có hộp kiểm

Ripple chỉ hiển thị khi nhấn và giữ, nhưng một lần nhấn đơn giản sẽ không hiển thị.

Có thể ai đó giúp tôi khắc phục sự cố? Cảm ơn trước.

PD: Tôi đang sử dụng ListView và bố cục bố cục mục là LinearLayout. Những gợn sóng đang hoạt động tốt. Sau khi chuyển sang RecyclerView, các mục gợn sóng không hoạt động. Tôi đã thử với LinearLayout một lần nữa, nhưng vẫn không hoạt động.

Dưới đây là file layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/requestCard" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?android:attr/selectableItemBackground" 
    android:clickable="true" 
    android:focusable="true" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/imgIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:adjustViewBounds="true" 
     android:maxHeight="64dp" 
     android:maxWidth="64dp" 
     android:padding="@dimen/lists_padding" 
     android:src="@drawable/ic_launcher" 
     tools:ignore="ContentDescription"/> 

    <TextView 
     android:id="@+id/txtName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@+id/imgIcon" 
     android:layout_toRightOf="@+id/imgIcon" 
     android:ellipsize="end" 
     android:maxLength="@integer/request_text_length" 
     android:maxLines="1" 
     android:padding="@dimen/lists_padding" 
     android:textSize="@dimen/abc_text_size_large_material" 
     tools:text="App Name"/> 

    <CheckBox 
     android:id="@+id/chkSelected" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:clickable="false" 
     android:padding="@dimen/lists_padding"/> 

</RelativeLayout> 
+0

Thêm android: descendantFocusability = "blocksDescendants" vào phần tử gốc của mục của bạn. –

+0

@GabrieleMariotti đã làm. Vẫn không làm việc. Đã cập nhật tệp bố cục. –

+0

Tôi không hoàn toàn chắc chắn, nhưng điều này có thể xảy ra vì các chế độ xem trong nhóm xem bao gồm hiệu ứng gợn sóng của bạn. Vì vậy, nó ở đó, nhưng bạn không thể nhìn thấy nó. Vì vậy, tôi đã kết thúc trong việc thêm một cái nhìn đơn giản thêm (với chiều rộng và chiều cao match_parent) đặc biệt cho gợn. Bạn có thể muốn thử. –

Trả lời

8

Trong phương pháp onClick của bạn (giả sử trong callback của bạn), chắc chắn bạn không gọi notifyDataSetChanged() sau notifyItemChanged(position).

thông báoDataSetChanged() sẽ xung đột với các hiệu ứng gợn sóng mặc định .

new recyclerAdapter.ClickListener() { 
    @Override 
    public void onClick(int position) { 

     ... awesome item onClick code ... 

     notifyItemChanged(position); 
     //notifyDataSetChanged(); <//--- Causes the no ripple bug 
    } 
}; 
0

Bạn sử dụng android:background="?android:attr/selectableItemBackground" nhưng bạn nên sử dụng android:foreground="?android:attr/selectableItemBackground"

+0

Đã thử. Vẫn không làm việc. –

+0

Tôi vừa thử bố cục của bạn trong dự án thử nghiệm và tất cả đều hoạt động tốt - https://drive.google.com/file/d/0B5KouoPaI5qqQWFRenVpRkRlQ3M/view?usp=sharing – curioushikhov

+0

@curioshikhov Tôi đã thử bố cục tương tự, vẫn không có gì ... Có nơi nào tôi có thể liên lạc với bạn và nhận trợ giúp của bạn không? Tôi không thể chia sẻ nguồn đầy đủ vì là riêng tư, nhưng nếu bạn sẵn sàng giúp tôi có thể làm điều đó, cảm ơn trước. –

1

Tôi nghĩ rằng bạn đang tìm kiếm effect.I gợn có thể cung cấp cho bạn ý tưởng cơ bản, Chỉ cần tạo một file xml (ripple.xml) trong drawable & ghi mã này bên

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?attr/colorControlHighlight"> 
    <item 
     android:id="@android:id/mask" 
     android:drawable="@android:color/white" 
     > 

    </item> 
</ripple> 

& chỉ cần viết hai dòng trong tệp xml khác của bạn mà bạn đã khai báo các phần tử recyclerview.

android:background="@drawable/ripple" 
android:clickable="true" 

Giống như trong trường hợp của tôi, tôi đang gặp chỉ TextView và trong thẻ cha mẹ tôi đang tuyên bố hai dòng sau

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

<TextView 
    android:id="@+id/textView_Username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="30sp" 
    android:textColor="@color/colorPrimary" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    /> 
</LinearLayout> 

Cuối cùng kết quả: enter image description here

Xem video này

https://www.youtube.com/watch?v=qvVkDb7DqCk

Các vấn đề liên quan