cymcsg / UltimateRecyclerView

A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.
Apache License 2.0
7.22k stars 1.43k forks source link

setOnClickListener not working #269

Open abhilash1in opened 8 years ago

abhilash1in commented 8 years ago
ultimateRecyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),EventDetailsActivity.class);
                (v.getContext()).startActivity(intent);
            }
        });

I want to open a new activity when an item of the ultimateRecyclerView is clicked on. I tried the above code but it doesn't seem to work. Nothing happens when I click any list item.

How should I implement an onClickListener for a list item in ultimateRecyclerView?

snakeice commented 8 years ago

This should be implemented in the Adapter, specifically in the function:

 public void onBindViewHolder (final UltimateRecyclerviewViewHolder holder, int position)
abhilash1in commented 8 years ago

I do not want to implement a click listener in the adapter since I have another recyclerview which has the same adapter, which has a different set of actions for an item click

snakeice commented 8 years ago

But you can move the listener recyclerview for adaper then only to set it on click..

I'm going through the Listener

 @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final UltimateRecyclerviewViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (position < getItemCount() && (customHeaderView != null ? position <= mValues.size() : position < mValues.size()) && (customHeaderView == null || position > 0)) {
            final ProtocolosListaAdapterSwipe.ViewHolder viewHolder = (ProtocolosListaAdapterSwipe.ViewHolder) holder;
            viewHolder.mItem = mValues.get(position);
            viewHolder.mIdProtocolo.setText(mValues.get(position).getId().toString());
            viewHolder.mTituloProtocolo.setText(mValues.get(position).getTitulo());
            List<Protocolo_interacoesEntity> interacoes = mValues.get(position).getProtocolo_interacoesEntityList();
            if (interacoes.size() > 0)
                viewHolder.mDescricaoInteracao1.setText(interacoes.get(interacoes.size() - 1).getInteracao());
            if (interacoes.size() > 1)
                viewHolder.mDescricaoInteracao1.setText(interacoes.get(interacoes.size() - 2).getInteracao());
            if (interacoes.size() > 2)
                viewHolder.mDescricaoInteracao1.setText(interacoes.get(interacoes.size() - 3).getInteracao());
            String s = mValues.get(position).getCountInteracoesNaoLidas() > 99 ? "99+" : String.valueOf(mValues.get(position).getCountInteracoesNaoLidas());
            viewHolder.mNumeroInteracoes.setText(s);
            if (!s.equals("0")) {
                viewHolder.mNumeroInteracoes.setVisibility(View.VISIBLE);
            } else {
                viewHolder.mNumeroInteracoes.setVisibility(View.INVISIBLE);
            }
            viewHolder.mView.findViewById(R.id.content_protocolo)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != mListener) mListener.onItemClick(viewHolder.mItem);
                        }
                    });
        }
        if (position > 4 && position > mLastPosition) {
            for (Animator anim : getAdapterAnimations(holder.itemView, AdapterAnimationType.ScaleIn)) {
                anim.setDuration(300).start();
                anim.setInterpolator(mInterpolator);
            }
            mLastPosition = position;
        } else {
            ViewHelper.clear(holder.itemView);
        }

    }