Firebase RecyclerView ClickListener Not Being Called from .onStart()

365 views Asked by At

I followed the post here. I am essentially trying to set up a click listener in my RecyclerView. I am getting no response when I click an item in the RecyclerView. I have logged it and I'm not getting a call to the log when I click. Therefore, I know the .onClick() method is not firing. I initialize the adapter in .onStart(), as seen below:

@Override
public void onStart() {
    super.onStart();

    mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
        @Override
        protected void populateViewHolder(PollHolder viewHolder, Poll model, final int position) {
            viewHolder.mPollQuestion.setText(model.getQuestion());
            Picasso.with(getActivity().getApplicationContext())
                    .load(model.getImage_URL())
                    .fit()
                    .into(viewHolder.mPollImage);
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.v("TAG","ONCLICKED");
                    Intent toPoll = new Intent(getActivity(), PollHostActivity.class);
                    toPoll.putExtra("POLL_ID", mFireAdapter.getRef(position).getKey());
                    startActivity(toPoll);

                }
            });
            Log.v("TAG", mFireAdapter.getRef(position).getKey());
            Log.v("TAG", String.valueOf(position));
            Log.v("TAG", model.getQuestion());
            Log.v("TAG", model.getImage_URL());
        }
    };

    mRecyclerview.setAdapter(mFireAdapter);

    mFireAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            int pollCount = mFireAdapter.getItemCount();
            int lastVisiblePosition = mLayoutManager.findLastCompletelyVisibleItemPosition();

            // If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
            // to the bottom of the list to show the newly added message.
            if (lastVisiblePosition == -1 ||
                    (positionStart >= (pollCount - 1) && lastVisiblePosition == (positionStart - 1))) {
                mRecyclerview.scrollToPosition(positionStart);
            }
        }
    });
}

Here is where I create my ViewHolder():

 public static class PollHolder extends RecyclerView.ViewHolder {

    TextView mPollQuestion;
    ImageView mPollImage;
    View mView;

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

        mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
        mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
        this.mView = itemView;
    }
}
2

There are 2 answers

0
tccpg288 On BEST ANSWER

Turns out this one line was screwing everything up and preventing the RecyclerView views from being clickable:

android:clickable="true"
4
Nandkishor mewara On

In the recyclerView you can set clicklistener in recyclerview Adapter this is working for me

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    ImageView imageView;
    public MyViewHolder(View view) {
        super(view);
        view.setOnClickListener(this); //clicklistner on recyclerview Item
        imageView = (ImageView) view.findViewById(R.id.grid_item_image);
    }

    @Override
    public void onClick(View v) {
        //Log.d(TAG, "onClick " + getPosition());
        Toast.makeText(activity, "onClick "+ getPosition(), Toast.LENGTH_SHORT).show();

    }
}