如何为onClick添加涟漪效果?

时间:2015-09-16 17:57:47

标签: android android-recyclerview rippledrawable

抽拉/ ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<!--pre Lollipop unable to ripple-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/clickstate" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</selector>

绘制-V21 / ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">


    <item
        android:id="@android:id/mask"
        android:drawable="@color/white"/>

</ripple>

layout / list_items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ripple"
    android:clickable="true"
    android:gravity="center_vertical"
    android:orientation="horizontal"/>

................

我尝试了上面的代码,在pre-L和Lollipop上添加了连锁效果 但是,只有当 onLongClickListener 无法看到 onClick

时,我才会产生连锁反应

问题是什么?这是我的java代码,它只显示了常见的recyclerview。

mRecyclerView.setAdapter(mAdapter);

        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(rootView.getContext(), DividerItemDecoration.VERTICAL_LIST);
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());


// Customized Click Listener
public static interface ClickListener {

        public void onClick(View view, int position);
        public void onLongClick(View view, int position);
    }
    static class  RecyclerTouchListener implements RecyclerView.OnItemTouchListener{

        private GestureDetector gestureDetector;
        private ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
            this.clickListener = clickListener;



            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onSingleTapUp(MotionEvent e) {

                        return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {


        }
    } 

0 个答案:

没有答案
相关问题