本文介绍了黄油刀@OnClick在CustomAdapter中的ViewHolder内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习黄油刀,我需要将特定的@OnClicks绑定到我的客户适配器生成的不同视图上.我该怎么办?

I'm currently learning butterknife, and I need to bind specific @OnClicks on different views generated by my customer adapter. How do I go about with this?

我在这里的一个问题中看到一个答案,说他在"ViewHolder"中使用了"@onclick".我不确定如何确切地实现这一点.有什么想法吗?

I've seen an answer in one of the questions here saying that he uses '@onclick' inside the 'ViewHolder'. I'm not sure how to implement this exactly. Any ideas?

谢谢!

推荐答案

假设您有一个这样的ViewHolder.

Imagine you have a ViewHolder like this.

static class ViewHolder {

    @InjectView(R.id.user_name)
    TextView userName;

    @InjectView(R.id.user_title)
    TextView userTitle;


    public ViewHolder(View view) {
        ButterKnife.inject(this, view);
    }

}

您可以在适配器的getView方法中为持有人中的每个视图绑定事件.

You can bind events in your adapters getView method for each view in your holder.

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView != null) {
        holder = (ViewHolder) convertView.getTag();
    } else {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.your_row_layout, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    }

   holder.userName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Your stuff here
        }
    });


    holder.userTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //Your stuff here  
        }
    });

    return convertView;

}

这篇关于黄油刀@OnClick在CustomAdapter中的ViewHolder内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 23:23