Recyclerview OnItem Click Listener无法正常工作

时间:2016-08-08 05:27:07

标签: android-recyclerview onitemclicklistener recycler-adapter

您正在使用recyclerview,其中onitem点击侦听器无法正常工作我也使用接口实现。 我的编码 在片段中:

public void setList() {

    int total = 0;
    for (CartRes item : cartBasket) {
    }

    final LinearLayoutManager layoutManager = new GridLayoutManager(context,2);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new NewAdapter(context, cartBasket, mListener, MYFRAGMENT, MyJavaClass.this);

    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

public interface CartListInterface {
    // TODO: Update argument type and name
    void onListFragmentInteraction(CartRes item);


}
@Override
public void onStop() {
    super.onStop();
    CommonUtil.dbUtil.close();
}

@Override
public void onStart() {
    super.onStart();
    CommonUtil.dbUtil.open();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof CartListInterface) {
        mListener = (CartListInterface) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnAccListFragInteractionListener");
    }


}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

和我的适配器:

    public NewAdapter(Context context, List<CartRes> cartBasket, MyJavaClass.CartListInterface mListener, String myfragment, MyJavaClass Cartfragment) {
    cartRes = cartBasket;
    MYFRAGMENT = myfragment;
    this.mListener = mListener;
    this.con = context;
    this.fragment = Cartfragment;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chicken_list, parent, false);
    CommonUtil.dbUtil = new DbUtil(con);
    CommonUtil.dbUtil.open();
    CommonUtil.dbHelper = new DbHelper(con);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    /*
    * Get the values from CartRes Class
    *
    * */
    if (null != mListener) {

        mListener.onListFragmentInteraction(holder.cartRess);
    } else{
        Log.e("edgarrr","No Listner");
    }

    holder.cartRess = cartRes.get(position);


    holder.prName.setText(cartRes.get(position).proname);

   holder.prName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
  mListener.onListFragmentInteraction(holder.cartRess);
   }

* */
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView prName, prate,prid,weight,itemid;
    public CardView cardlist_item;
    LinearLayout linearBG;
    ImageView bgImage,imgCart,offer;
    Spinner spinner1;
    CartRes cartRess;


    public ViewHolder(View itemView) {
        super(itemView);

        itemView.setClickable(true);
        itemView.setOnClickListener(this);
        cardlist_item = (CardView) itemView.findViewById(R.id.cardlist_item1);

        prName = (TextView) itemView.findViewById(R.id.ShpName);
        prate = (TextView) itemView.findViewById(R.id.rate);




        linearBG = (LinearLayout) itemView.findViewById(R.id.linearBG);
        bgImage = (ImageView) itemView.findViewById(R.id.imageView);





    }


    @Override
    public void onClick(View view) {
        mListener.onListFragmentInteraction(cartRess);

    }
}
谁能帮我完成这个,我不知道我错在哪里。请指出我的错误,我尝试了多种方式在这里搜索工具,onitem点击监听器想在适配器中的bindholder中实现

1 个答案:

答案 0 :(得分:1)

将onClickListener更改为此并再试一次:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    holder.cartRess = cartRes.get(position);
    holder.prName.setText(cartRes.get(position).proname);

}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView prName, prate,prid,weight,itemid;
    public CardView cardlist_item;
    LinearLayout linearBG;
    ImageView bgImage,imgCart,offer;
    Spinner spinner1;
    CartRes cartRess;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setClickable(true);
        itemView.setOnClickListener(this);
        cardlist_item = (CardView)    
        itemView.findViewById(R.id.cardlist_item1);
        prName = (TextView) itemView.findViewById(R.id.ShpName);
        prate = (TextView) itemView.findViewById(R.id.rate);
        linearBG = (LinearLayout) itemView.findViewById(R.id.linearBG);
        bgImage = (ImageView) itemView.findViewById(R.id.imageView);
    }

       @Override
       public void onClick(View view) {
            int selectedPosition = getAdapterPosition();
            if( selectedPosition != RecyclerView.NO_POSITION )      
            {
              CartRes item = cartBasket.get(selectedPostion);
              mListener.onListFragmentInteraction(item);
            }
       }
}