首先,在adapter类中声明一个接口,使用接口回调进行点击事件的回传
private SubClickListener subClickListener;
在adapter中对按钮进行点击事件监听,并通过接口方法回传数据
holder.ibtn_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (subClickListener != null) {
subClickListener.OntopicClickListener(v, mList.get(position), position);
}
}
});;
在adapter中定义接口和方法
public void setsubClickListener(SubClickListener topicClickListener) {
this.subClickListener = topicClickListener;
}
public interface SubClickListener {
void OntopicClickListener(View v, MainDetail detail, int position);
}
adapter.setsubClickListener(new SubClickListener() {
@Override
public void OntopicClickListener(View v, MainDetail detail, int position) {
isSubscribe = (ImageButton) v.findViewById(R.id.iv_item_unfollow_anchor);
if ("1".equals(detail.follow)) {
if ((String) SpUtils.get(getContext(), "voice_token", "") != null
&& (String) SpUtils.get(getContext(), "voice_token", "") != "") {
delFollow(detail.id, "");
isSubscribe.setImageResource(R.drawable.activity_anchor_attention);
} else {
CommonUtils.isExistTicket(getActivity());
}
} else {
if ((String) SpUtils.get(getContext(), "voice_token", "") != null
&& (String) SpUtils.get(getContext(), "voice_token", "") != "") {
addFollow(detail.id, "");
Toast.makeText(getContext(), "关注成功", 0).show();
isSubscribe.setImageResource(R.drawable.activity_anchor_attentioned);
} else {
CommonUtils.isExistTicket(getActivity());
}
}
}
});