如何在Android中的recyclerView中处理多个布局点击

时间:2015-04-17 11:53:17

标签: android onclicklistener android-recyclerview

在我们的应用中,我们有消息列表。我们正在切换到RecyclerView。 我们的消息可以包括文本,图像。 示例消息可能如下所示。 enter image description here

要处理点击次数,请使用此课程:

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

我们在此类活动中实施此次点击:

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的{​​{1}}次点击。是否可以检测从GestureDetection点击哪个列表项布局?

换句话说,我可以点击GestureDetector点击的孩子。在我的情况下,点击设置为整个项目(头像,短信,图像)。我可以检测此项目中的哪个元素被点击(例如我想要点击头像)。

6 个答案:

答案 0 :(得分:3)

处理RecyclerView适配器中的点击,如下例所示:

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


        }
    }

答案 1 :(得分:0)

您可以尝试以下代码:

GestureDetector detector;

然后:

detector = new GestureDetector(MainActivity.this, MainActivity.this);

    avatar.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View view, MotionEvent e) {
            detector.onTouchEvent(e);
            return false;
        }
    });

答案 2 :(得分:0)

您可以将侦听器设置为适配器类。这是代码:

适配器内的ViewHolder类:

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textViewFirstName;
    ImageView imageViewProfile;

    public MyViewHolder(View view) {
        super(view);
        textViewFirstName = (TextView) view.findViewById(R.id.txtName);
        imageViewProfile = (ImageView) view.findViewById(R.id.imageViewProfile);

//setOnClick
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    //check if the image you want to click should not invoke listener from activity/fragment
        if (v.getId() != R.id.imageViewProfile) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getPosition());
            }
        }
    }
}

设置imageView单击Adapter类onBindViewHolder:

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

    holder.textViewFirstName.setText(mArrayListTypeDetials.get(position).getFirstname());
    //set imageView click here
    holder.imageViewProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //ImageView clicked
        }
    });
}

在Adapter类中创建接口:

//interface for recyclerView item click
public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

/*
method to set listener to the adapter ViewHolder item
 */
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}

从活动/片段设置界面:

    mAdapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getActivity(), ABC.class);
            startActivity(intent);
        }
    });

答案 3 :(得分:0)

based on this post.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int key = new Random().Next();
            ServerKey = key;
            ClientKey.Value = key.ToString();
        }
    }
protected void OnClick(object sender, EventArgs e)
    {
        // Decrypt(txtData.Text, ServerKey.ToString()));
    }

}

答案 4 :(得分:0)

我试过像@ user320676这样的方式,但我试着用一个监听器来完成:

interface MultipleClickListener {
        void onItemClickOne(int position, View v);
        void onItemClickTwo(int position, View v);
        void onItemClickThree(int position, View v);
}

然后您可以在活动或片段中使用侦听器。在这里你可以看到我的完整适配器:

public class MultipleClicksAdapter extends RecyclerView
        .Adapter<MultipleClicksAdapter
        .DataObjectHolder> {

    private ArrayList<MultipleClickData> mDataset;
    private static MultipleClickListener sClickListener;

    static class DataObjectHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.multiple_clicks_one_textView)
        TextView mOne;
        @BindView(R.id.multiple_clicks_two_textView)
        TextView mTwo;
        @BindView(R.id.multiple_clicks_three_textView)
        TextView mThree;

        DataObjectHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);

            mOne.setOnClickListener((View v) ->
                    sClickListener.onItemClickOne(getAdapterPosition(), mOne));

            mTwo.setOnClickListener((View v) ->
                    sClickListener.onItemClickTwo(getAdapterPosition(), mTwo));

            mThree.setOnClickListener((View v) ->
                    sClickListener.onItemClickThree(getAdapterPosition(), mThree));
        }

    }

    void setOnItemClickListener(MultipleClickListener myClickListener) {
        this.sClickListener = myClickListener;
    }

    MultipleClicksAdapter(ArrayList<MultipleClickData> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public MultipleClicksAdapter.DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.multiple_clicks_list_item, parent, false);

        MultipleClicksAdapter.DataObjectHolder dataObjectHolder = new MultipleClicksAdapter.DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(MultipleClicksAdapter.DataObjectHolder holder, int position) {
        holder.mOne.setText(mDataset.get(position).getmTitle());
        holder.mTwo.setText(mDataset.get(position).getmSubTitle());
        holder.mThree.setText(mDataset.get(position).getmThirdTitle());
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    interface MultipleClickListener {
        void onItemClickOne(int position, View v);
        void onItemClickTwo(int position, View v);
        void onItemClickThree(int position, View v);
    }

}

您也可以查看complete code in GitHub

答案 5 :(得分:-1)

点击需要在 RecyclerView 的适配器中处理,访问RecyclerView示例,类似于 ListView 的<{1}}