2015-06-24 17 views
12

Tôi có items.xml nàyRecyclerView android: attr/selectableItemBackground không hoạt động trên các mặt hàng

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

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:id="@+id/colorPreview" /> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:textAppearance="@android:style/TextAppearance.Large" 
    android:textColor="@android:color/white" 
    android:id="@+id/colorName" /> 

</RelativeLayout> 

Khi tôi sử dụng nó một cách riêng biệt, các selectableItemBackground sinh động khi tôi bấm vào xem. Nhưng khi tôi sử dụng nó cho các mục trong RecyclerView, hiệu ứng khi nhấp chuột không xảy ra nữa. Làm thế nào tôi có thể sửa lỗi này?

PS: đây là người biết lắng nghe trên RecyclerView, nếu nó là có liên quan:

public ColorListOnItemTouchListener(Context context, OnItemClickListener clickListener) { 
    mClickListener = clickListener; 
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public void onLongPress(MotionEvent e) { 
       if(childView != null && mClickListener != null) { 
        mClickListener.onItemLongPress(childView, index); 
       } 
     } 

     @Override 
     public boolean onSingleTapUp(MotionEvent e) { 
      if(childView != null && mClickListener != null) { 
       mClickListener.onItemClick(childView, index); 
      } 
      return true; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      return false; 
     } 
    }); 
} 

Cảm ơn bạn!

Edit:

public class ColorsCursorAdapter extends RecyclerView.Adapter<ColorsCursorAdapter.ViewHolder> { 

    private static final int layout = R.layout.color_item; 
    private Cursor mCursor; 

    public ColorsCursorAdapter(Cursor c) { 
     super(); 
     this.mCursor = c; 
    } 


    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false); 
     TextView name = (TextView) v.findViewById(R.id.colorName); 
     ImageView image = (ImageView) v.findViewById(R.id.colorPreview); 
     return new ViewHolder(v, name, image); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     mCursor.moveToPosition(position); 
     int color = mCursor.getInt(mCursor.getColumnIndex(ColorItem.COLUMN_COLOR)); 
     holder.colorName.setText(Utils.getColorString(color)); 
     holder.colorPreview.setImageDrawable(new ColorDrawable(color)); 
    } 

    @Override 
    public int getItemCount() { 
     if(mCursor != null) { 
      return mCursor.getCount(); 
     } 
     return 0; 
    } 

    public void swapCursor(Cursor c) { 
     mCursor = c; 
     notifyDataSetChanged(); 
    } 

    public Cursor getCursor() { 
     return mCursor; 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 
     public TextView colorName; 
     public ImageView colorPreview; 

     public ViewHolder(View root, TextView colorName, ImageView colorPreview) { 
      super(root); 
      root.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // 
       } 
      }); 
      this.colorName = colorName; 
      this.colorPreview = colorPreview; 
     } 
    } 
} 

Và adapter được tạo ra với:

colorList.setLayoutManager(new LinearLayoutManager(this)); 
    adapter = new ColorsCursorAdapter(null); 
    colorList.setAdapter(adapter); 
+0

Bạn đã thử đặt trên trình lắng ngheClick trên holderView trong bộ điều hợp? – Hellboy

+0

Tôi đã làm ... và nó không hoạt động –

+0

nhưng bạn có đặt người nghe trên một chế độ xem có bộ chọn có thể chọn lại không? – Hellboy

Trả lời

4

Trong items.xml của bạn, thiết lập FrameLayout như cách bố trí gốc và thiết lập selectableItemBackground cho FrameLayout. Nó làm việc cho tôi, nhưng tôi không biết tại sao.

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:foreground="?android:selectableItemBackground"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <!-- your code --> 
    </RelativeLayout> 
</FrameLayout> 
+1

Lý do cho điều này là FrameLayout hỗ trợ thuộc tính 'foreground' ở cấp API thấp hơn 23. Các chế độ xem khác chỉ hỗ trợ bắt đầu cấp API 23. – ubuntudroid

+0

Sử dụng kết hợp này với 'android: clickable =" true "' làm việc cho tôi – Aba

12

Thay vì đặt làm nền, tôi đặt nó làm nền trước, nó hoạt động. Hy vọng nó sẽ hữu ích.

android:foreground="?attr/selectableItemBackground" 
+5

Tôi không nghĩ rằng nó sẽ hoạt động trên API dưới 23 mặc dù. –

+0

@VarvaraKalinina là có một cách để làm cho nó hoạt động cho các cấp api dưới 23? –

25

Cái gì đó đã không được đề cập trong câu trả lời khác: thiết android:clickable="true" là cần thiết để làm cho hình ảnh động làm việc khi không có OnClickListener gắn liền với quan điểm.

0

Tôi đã thấy hành vi tương tự. Về phía tôi, nó liên quan đến thực tế là khi tôi nhấp vào một mục danh sách trong một đoạn và thay thế trực tiếp đoạn đó với một đoạn khác, hoạt hình không có thời gian để được hiển thị. Ngay sau khi tôi loại bỏ các thay thế mảnh, tôi có thể nhìn thấy các hình ảnh động.

0
@Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     if(childView != null && mClickListener != null) { 
      mClickListener.onItemClick(childView, index); 
     } 
     return true; 
    } 

Trả về lỗi có thể khắc phục được sự cố này. Đối với tôi, tôi đã ghi đè lên nút bấm trên nút của tôi và trở lại đúng vào cuối. Trả về false đã bật hoạt ảnh chạm để xảy ra từ cài đặt:

android:background="?attr/selectableItemBackground" 
0

Trong trường hợp của tôi sự cố là do DividerItemDecoration. Ngay sau khi tôi thoát khỏi điều đó, nó bắt đầu hoạt động. Tôi đề nghị đặt một dòng vào cuối xml tệp cho mỗi mục trong RecyclerView, việc tùy chỉnh dễ dàng hơn nhiều và bạn sẽ không gặp phải các loại vấn đề này.

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