当我为RecyclerView设置onTouchListener并为RecyclerView的childView设置onClickListener时,

时间:2016-11-17 06:53:57

标签: android android-layout android-recyclerview

首先,我为onTouchListener设置recyclerView

recyclerview.setOnTouchListener(this);
@Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    System.out.println("action_down");
                    if (layoutManager instanceof GridLayoutManager) {
                        GridLayoutManager g = ((GridLayoutManager) layoutManager);
                        System.out.println("findFirstVisibleItemPosition = " + g.findFirstVisibleItemPosition());
                        if (g.findFirstVisibleItemPosition() == 0) {
                            isTop = true;
                            startY = (int) event.getY();
                        }
                    } else if (layoutManager instanceof LinearLayoutManager) {
                        LinearLayoutManager l = (LinearLayoutManager) layoutManager;
                        System.out.println("findFirstVisibleItemPosition = " + l.findFirstVisibleItemPosition());
                        if (l.findFirstVisibleItemPosition() == 0) {
                            isTop = true;
                            startY = (int) event.getY();
                        }
                    }
                }
                break;
                case MotionEvent.ACTION_MOVE: {
                    System.out.println("action_move");
                }
                break;
                case MotionEvent.ACTION_UP: {
                    System.out.println("action_up");
                    if (state == RELEASE) {
                        state = REFRESHING;
                        refreshState();
                        listener.onRefreshing();
                    } else if (state == PULL) {
                        state = NORMAL;
                        isTop = false;
                        resetRefresh(paddingBottom, -headerRefreshHeight, 400);
                    }
                }
                break;
            }
            return false;
        }

我在适配器的onClickListener()方法

中为childView RecyclerView设置了onBindViewHolder()
@Override
    public void onBindViewHolder(RViewHolder holder, final int position) {
        final int pos = position;
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, pos);
                }
            });
        }

        if (onItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return onItemLongClickListener.onItemLongClickListener(v, position);
                }
            });
        }
        convert(holder, data.get(position));
    }

最后在onTouch()函数中,不再有action_down MotionEvent。 我弄清楚了。谁知道呢?非常感谢提前!

0 个答案:

没有答案