Where should I place setOnClickListener in a Recyc

2020-07-06 04:09发布

In tutorials on internet where they setOnClickListener in Adapter of RecyclerView they define it in two ways : either inside ViewHolder or inside BindViewHolder.

My Question is which one is a better approach, Please recommend any another approach if available

1) inside ViewHolder:

public static class ViewHolder extends RecyclerView.ViewHolder {

    public ViewHolder(View itemView) {
        super(itemView);
        tvSrc = (TextView) itemView.findViewById(R.id.tvSrc);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });
    }

2) inside BindViewHolder

public void onBindViewHolder(DisplayTrainsAdapter.ViewHolder viewHolder, final int position) {

    viewHolder.tvSrc.setText(mDataset.get(position).strSrc);
    viewHolder.tvSrc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(v.getContext(), "position = " + getItemId(position), Toast.LENGTH_SHORT).show();
        }
    });
}    

5条回答
Explosion°爆炸
2楼-- · 2020-07-06 04:34

IMHO: I like number 1.

Since your calling into new ViewHolder(View) your really setting your onClickListener before actually displaying your content. This is nice because by the time onBindView is called, your onClickListener has already been set on your view.

I think its also cleaner code to do this in your constructor ViewHolder(View)

查看更多
Animai°情兽
3楼-- · 2020-07-06 04:49

Both options have their pros and cons.

For example, if a Button is clicked and you want to change the text of your button, then you should probably use the option where you setup the onClick listener in the ViewHolder. Apart from this reason, it also makes your code look cleaner.

However, if say, when the Button is clicked, you want to change the text of a TextView in the same index/position as the button that was clicked, you will need to use the option where you setup the onClick listener in the onBindViewHolder method.

查看更多
4楼-- · 2020-07-06 04:51

In ViewHolder() i guess, since you define what view is holded and what it has inside and other functions.

But onBindViewHolder() you say that view, which is defined in ViewHolder, will have this text, this image...

查看更多
聊天终结者
5楼-- · 2020-07-06 04:56

You should always check if getAdapterPosition is >= 0 because it could be -1 (NO_POSITION) in rare cases that can lead to a crash in your app. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder#getadapterposition

查看更多
beautiful°
6楼-- · 2020-07-06 04:59

Your number 1 solution is the best as you proposed since that assignment will not be called in the Binding at each invalidate triggered by notify..() methods. I know also others solutions but you need to implement android.view.GestureDetector in your activity.

If you want others improvements on the adapter, take a look at mine FlexibleAdapter https://github.com/davideas/FlexibleAdapter and feel free to implement in your project.

查看更多
登录 后发表回答