在RecyclerView中触摸动画并不起作用

时间:2015-12-02 08:02:26

标签: android android-animation android-recyclerview

在我的列表项目xml中,我设置了这段代码:

android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"

当我按下RecyclerView项目时会产生一些波纹动画。
为了获得项目,我使用了这个类:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
}

并像这个例子一样使用它:

rv.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Log.d("OnClick", "click at " + position);
                    Intent intent = new Intent(getActivity(), DetailEventActivity.class);
                    intent.putExtra("EVENT", list.get(position - 1));
                    startActivity(intent);
                }
            }));

这是我使用RecyclerView的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"
        />

</RelativeLayout>

这是我的项目布局的布局:

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

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="20dp"
        android:id="@+id/event_card_icon"
        android:src="@mipmap/ic_launcher" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.devspark.robototextview.widget.RobotoTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add 500 rub"
            android:textSize="17sp"
            app:typeface="roboto_light"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="72dp"
            android:textColor="#7a7a7a"
            android:id="@+id/event_description"
            android:layout_toLeftOf="@+id/ic_replay"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <com.devspark.robototextview.widget.RobotoTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/time_of_publish"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="72dp"
            android:text="@string/time"
            android:textColor="#b8b8b8"
            app:typeface="roboto_regular"
            android:layout_below="@id/event_description"/>

        <com.devspark.robototextview.widget.RobotoTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+4"
            android:textSize="12sp"
            app:typeface="roboto_regular"
            android:drawableRight="@drawable/ic_points_small"
            android:textColor="#cccccc"
            android:drawablePadding="5dp"
            android:layout_alignBottom="@+id/time_of_publish"
            android:layout_alignParentRight="true"
            android:layout_marginRight="16dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ic_replay"
            android:layout_alignTop="@+id/event_description"
            android:layout_marginRight="16dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/time_of_publish"
            android:id="@+id/divider2"
            android:layout_marginLeft="72dp"
            android:layout_marginTop="20dp"
            android:background="#ebebeb"
            />

    </RelativeLayout>

</RelativeLayout>

我看到很多问题和答案,而且我不知道为什么这段代码不起作用。也许有人可以帮助我。

我的问题是在RecyclerView项目上按动画对我不起作用。

1 个答案:

答案 0 :(得分:0)

在drawable-v21文件夹中创建一个可绘制的波纹,id掩码用于限制所单击行的边界内的波纹效果: ripple_drawable.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
  android:color="your rippleeffect color">
<item android:id="@android:id/mask"
     android:drawable="@android:color/white" />

在你的项目布局中,相对布局将android:background属性更改为

android:background="@drawable/ripple_drawable"