当前位置: 动力学知识库 > 问答 > 编程问答 >

android - RecyclerView - Where should I handle its click events?

问题描述:

Prior to the introduction of RecyclerView (and its mandatory ViewHolder pattern), I usually delegate any click events to its corresponding Activity/Fragment using setOnItemClickListener(). (Because I mainly see Activity/Fragment as a "controller" object when developing for Android, thus any modification to the view should be done in it.)

Now, as RecyclerView doesn't really treat its children the same way and that setOnItemClickListener() (or similar) methods are no longer implemented for it - where should I handle click events that may take place? I don't know.. but handling them in an Adapter seems awkward to me.

How are we supposed to do it?

Thanks in advance!

网友答案:

Create your own viewHolder for the recycler view as we always do it, and in the onBindView method, set the click listener to the view you wish to perform the click.

@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
viewHolder.mRelContent.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // perform ur click here
        }
    });
}
网友答案:

See Jacob's implementation of RecyclerView.OnItemTouchListener. I think it's the best solution.

Hope it will help you. Regards.

分享给朋友:
您可能感兴趣的文章:
随机阅读: