0

I'm trying to set an onclick method in my RecyclerView. It worked on another project but not here. I searched on the web but can't figure it out. Not even the toast is popping up. Can someone, please, explain to me where I am making a mistake?

Here's my code:

private Context mContext;
private MenuImages[] mMenuImages;

public MenuAdapter(Context context, MenuImages[] menuImages){
    mContext = context;
    mMenuImages = menuImages;
}

@Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.menu_list_item, parent, false);
    MenuViewHolder viewHolder = new MenuViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
    holder.bindMenu(mMenuImages[position]);
}

@Override
public int getItemCount() {
    return mMenuImages.length;
}

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

    public ImageView mImageView;
    public TextView mTextView;

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

        mImageView = (ImageView) itemView.findViewById(R.id.itemImageView);
        mTextView  = (TextView) itemView.findViewById(R.id.textView2);

        itemView.setOnClickListener(this);
    }

    public void bindMenu(MenuImages menuImage){
            mImageView.setImageBitmap(menuImage.getImageMenu());
            mTextView.setText(menuImage.getTitleImageMenu());
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), getLayoutPosition(), Toast.LENGTH_LONG).show();
    }
}
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
samuel zaffran
  • 157
  • 1
  • 3
  • 13
  • you missed itemView.setClickable(true); – Arfan Mirza Oct 18 '16 at 20:52
  • Thank's for the quick answer, but still is not working, when i click, in my monitor i get those messages : W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=481.59375, y[0]=620.4297, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=72911, downTime=65936, deviceId=0, source=0x1002 } – samuel zaffran Oct 18 '16 at 20:58
  • Cancelling event due to no window focus :: => .setFocusable(true); :) – Arfan Mirza Oct 18 '16 at 21:04
  • 2
    check your R.layout.menu_list_item ,, may be checked click able in xml for image or text or may be for parent view... if child is click able then whole cell click not work.. i guess – Arfan Mirza Oct 18 '16 at 21:06
  • I think you're not close cause now i have an error, not a warning anymore that's saying Resources$NotFoundException: String resource ID #0x0 – samuel zaffran Oct 18 '16 at 21:20
  • This means he has found the solution. Use `String.valueOf(getLayoutPosition())` to convert the integer to a String. The int is interpreted as a resource id to android, which isn't valid. – zgc7009 Oct 18 '16 at 21:29
  • 1
    Thank you so much, you're awesome. First post and really surprised by this community. For those who may be have the same problem, it wasn't about setting focusable or clickable to true in java code but in the xml. I checked those in a child. – samuel zaffran Oct 18 '16 at 21:35
  • Just another thing, everything works great but, i still have this message in my monitor when i click for the first time on an item : W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=481.59375, y[0]=620.4297, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=72911, downTime=65936, deviceId=0, source=0x1002 } Does i have to be concern by it ? And how not having this message ? – samuel zaffran Oct 18 '16 at 21:45
  • Welcome to SO, we try to be helpful :) Take a look here for your last comment http://stackoverflow.com/questions/37823612/w-viewrootimpl-cancelling-event-due-to-no-window-focus-motionevent – zgc7009 Oct 18 '16 at 22:07

2 Answers2

1

in my case I was using RecyclerView in InterceptorCardView of other class wich was preventing onClickListener of recyclerview, when I changed it with CardView and it worked perfectly

Note: I removed clickable = "false" ,focusable = "false".

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
0

it may be late but in your layout xml file you should add attribute clickable = "false" and also focusable = "false" to the children view ,textview and imageview in your case

Karam Melad
  • 205
  • 2
  • 9