如何在Android中使用RecyclerView在ImageButton上setonItemClickListner

时间:2016-11-02 13:50:42

标签: android android-recyclerview

我正在尝试使用用户定义的界面实现OnItemClick on image按钮。

这是我在ImageButton Click上使用的用户定义界面。

public interface OnItemClickListener {

public boolean onItemClick(int position,HashMap<ArrayList<String>,ArrayList<String>> ash);

}

这是RecyclerView适配器

 public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView id, name, price, txt_counter, txt_CalculatePrice;
    Spinner spinner;
    Button add, minus;
    ImageView favoriteImg;
    int quantity = 0;
    LinearLayout mLayout;

    public MyViewHolder(final View itemView) {
        super(itemView);


        favoriteImg = (ImageView) itemView.findViewById(R.id.imgbtn_favorite);
        txt_counter = (TextView) itemView.findViewById(R.id.txt_Quantity);
        txt_CalculatePrice = (TextView) itemView.findViewById(R.id.price_Calculation);
        mLayout = (LinearLayout) itemView.findViewById(R.id.button_holder);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(v, getLayoutPosition(), uniqueData);
        }
    }
}

public interface OnItemClickListner {
    public void onItemClick(View view, int Position, HashMap<ArrayList<String>, ArrayList<String>> ash);
}

public void SetOnItemClickListener(OnItemClickListner mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}

这是片段

productListAdapter.SetOnItemClickListener(new ProductListAdapter.OnItemClickListner() {
                @Override
                public void onItemClick(View view, int Position, HashMap<ArrayList<String>, ArrayList<String>> ash) {
                    AddToDatabase(view,Position,ash,list,hashmap,dataArrayList);
                }
            });
            productListAdapter.notifyDataSetChanged();
            productListView.setAdapter(productListAdapter);

        }

我的当前代码工作正常,但是代码

productListAdapter.SetOnItemClickListener(new ProductListAdapter.OnItemClickListner()

导致我完成一次单击(完整视图),而我正在寻找特定图像按钮单击。

1 个答案:

答案 0 :(得分:1)

只需更改+中的以下内容:

即可
ViewHolder

对此:

itemView.setOnClickListener(this);