0

I have a RecyclerView which I'm populating with items as described by the layout below.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cv_item_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:weightSum="9">
    .
    .
    .
    </LinearLayout>
</android.support.v7.widget.CardView>

Whenever the CardView referenced by cv_item_display is clicked inside RecyclerView, I see ripple effect on Card BUT when I attach an OnClickListener to the card, I don't get ripple effect any more. Below is the code for ViewHolder where I attach OnClickListener.

public class RosterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView tvStudentCount, tvRosterName;
    ImageView ivDelete, ivView;
    CardView cvItemDisplay;

    public RosterViewHolder(View itemView) {
        super(itemView);
        tvStudentCount = (TextView) itemView.findViewById(R.id.tv_question_student_count);
        tvRosterName = (TextView) itemView.findViewById(R.id.tv_questionnaire_roster_name);
        ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
        ivView = (ImageView) itemView.findViewById(R.id.iv_view);
        cvItemDisplay = (CardView) itemView.findViewById(R.id.cv_item_display);
        cvItemDisplay.setOnClickListener(this);
        ivDelete.setOnClickListener(this);
        ivView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        final int position = getAdapterPosition();
        switch (view.getId()) {
            case R.id.iv_delete:
               mRealm.executeTransaction(new Realm.Transaction() {
                   @Override
                   public void execute(Realm realm) {
                       mRealm.where(RosterEntry.class).equalTo("rosterId", mRealmResults.get(position).getId()).findAll().deleteAllFromRealm();
                       mRealmResults.deleteFromRealm(position);
                   }
               });
               break;

            case R.id.iv_view:
               Utils.startRosterItemsActivity(view.getContext(), mRealmResults.get(position).getName(), mRealmResults.get(position).getId(), false);
               break;

            case R.id.cv_item_display:
               startAttendanceActivity(mRealmResults.get(position).getId());
               break;
        }
    }
}

EDIT: I also identified some weird behavior - ripple effect shows in landscape mode but not in portrait mode.

Abhinav Tripathi
  • 168
  • 1
  • 2
  • 12
  • wrap your `cardView` with a `relativeLayout` and set `onClickListener` to that `relativeLayout` – tahsinRupam Apr 16 '17 at 06:46
  • http://stackoverflow.com/q/26942434/7012517 may this question helps you..! – Shobhit Apr 16 '17 at 07:03
  • @Shobhit : That is a different question. My problem is with CardView with a clickListener inside a RecyclerView and without ClickListener it works fine. – Abhinav Tripathi Apr 16 '17 at 07:07
  • @tahsinRupam Wrapping in a relative layout doesn't help. If I wrap CardView in a relative layout, the relative layout doesn't receive click event unless I make the CardView ```clickable = false``` If I make CardView non clickable, ripple effect disappears. – Abhinav Tripathi Apr 16 '17 at 08:07

0 Answers0