Where should I place setOnClickListener in a RecyclerView Adapter

此生再无相见时 提交于 2019-12-21 09:24:59

问题


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

回答1:


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.




回答2:


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.




回答3:


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)




回答4:


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:


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



来源:https://stackoverflow.com/questions/29537375/where-should-i-place-setonclicklistener-in-a-recyclerview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!