本文介绍了如何处理多个布局点击在recyclerView Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序,我们有信息列表。我们切换到 RecyclerView 。我们的消息可以包括从文本,图像。样品信息可以是这样的。

In our app we have list of messages. We are switching to RecyclerView.Our messages can consist from texts, images.Sample message can look like this.

要处理的点击使用这个类:

To handle clicks use this class:

public class RecyclerViewItemClickListener implement  RecyclerView.OnItemTouchListener {

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

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

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

            @Override
            public void onLongPress(MotionEvent e)
            {
                View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if(childView != null && mListener != null)
                {
                    mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
                }
            }
        });
    }

    @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) {
    }
}

和我们实现这个点击的活动是这样的:

and we implement this clicks in activity like this:

chatView.setLongClickable(true);
        chatView.addOnItemTouchListener(
                new RecyclerViewItemClickListener(this, chatView, new RecyclerViewItemClickListener.OnItemClickListener() {
                    @Override public void onItemClick(View view, int position) {

                        //handleSingleClick(view, position);

                    }
                    @Override
                    public void onItemLongClick(View view, int position)
                    {
                        handleLongPress(position);
                    }
                })
        );

从同一个

点击消息是工作,现在我想还可以处理 userAvatar 点击 GestureDetection 。是否有可能检测哪些列表项目布局已从 GestureDetector

Clicks for message is working, now I want also handle userAvatar clicks from same GestureDetection. Is it possible to detect which list item layout was clicked from GestureDetector?

在换句话说,我可以得到 GestureDetecter 点击子。在我的情况下,点击设置为整个项目(头像,文字信息,图像)。我可以检测到这个项目里面的元素被点击了(比如我要赶头像点击)。

In other words, can I get GestureDetecter clicked child. In my case click is set to whole item(avatar, text message, image). Can I detect which element inside this item was clicked(for example I want catch avatar click).

推荐答案

处理click在RecyclerView的适配器像下面的例子

handle the click in adapter of your RecyclerView like below example

private class BookmarkViewHolder extends RecyclerView.ViewHolder {

        protected ImageView thumbnailIcon;
        protected TextView postTitle;
        protected TextView postViewCount;

        public BookmarkViewHolder(View v) {
            super(v);

            thumbnailIcon = (ImageView) v.findViewById(R.id.thumbnailIcon);
            postTitle = (TextView) v.findViewById(R.id.postTitleTV);
            postViewCount = (TextView) v.findViewById(R.id.viewCountTV);

            v.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                }
            });

            thumbnailIcon.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                }
            });

            postTitle.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                }
            });


        }
    }

这篇关于如何处理多个布局点击在recyclerView Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:59