onItemClick返回不同的值

时间:2018-06-29 11:15:58

标签: android view onitemclicklistener onitemclick

我的适配器中装有ViewHolder。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView optionImage;
        ImageView optionAddFavorites;
        BMLTextView optionText;
        RelativeLayout lay_icon;

        ViewHolder(View itemView) {
            super(itemView);
            optionImage = (ImageView) itemView.findViewById(R.id.img_option);
            optionAddFavorites = (ImageView) itemView.findViewById(R.id.iv_add_favorites);
            optionText = (BMLTextView) itemView.findViewById(R.id.txt_option_description);
            lay_icon = (RelativeLayout) itemView.findViewById(R.id.lay_icon);

            itemView.setOnClickListener(this);

            this.setIsRecyclable(false);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

稍后,我在“活动”中实现了:

public class OperateOptionSectionItemHelperImpl
        implements OperateOptionRVAdapter.ItemClickListener, OperateOptionSectionItemHelper {

我已经完成了onItemClick方法的覆盖,如下所示:

@Override
    public void onItemClick(View view, int position) {
    if (isEditingFavorites) {

        switch (view.getId()) {

            case R.id.iv_add_favorites:
                Log.e("onItemClick", "Add to favorites iv_add_favorites: " + R.id.iv_add_favorites + " parent: " + view.getId());

                break;
            case R.id.img_option:
                Log.e("onItemClick", "Add to favorites img_option: " + R.id.img_option + " parent: " + view.getId());

                break;

            default:
                Log.e("onItemClick", "Default");

        }

    } else {
        Log.e("onItemClick", "Rest of the view");

        }
    }
}

我没有点击视图的ID ...我在做什么错了?

1 个答案:

答案 0 :(得分:1)

那是因为您将ClickListener设置为itemView。面对现实,您应该将ClickListener设置为optionImage和optionAddFavorites 更改

itemView.setOnClickListener(this);

optionImage.setOnClickListener(this);
optionAddFavorites.setOnClickListener(this);