0

I'm developing an application with multiple timers based on card design, and I need to add three possible interactions with them:

  1. One tap to toggle between Start/Pause
  2. Double tap to reset the timer to its original value
  3. Long click to remove it

I can't figure out how to add the onClickListener and where to. This is my code:

CardAdapter.java

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {

    private ArrayList<Timer> data;

    public CardAdapter(ArrayList<Timer> data) {
        this.data = data;
    }

    @Override
    public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CardViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_timer, parent, false));
    }

    @Override
    public void onBindViewHolder(CardViewHolder holder, int position) {
        Timer timer = data.get(position);
        holder.timerName.setText(timer.getName());
        holder.timerTime.setText(timer.getTimeDisplay());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class CardViewHolder extends RecyclerView.ViewHolder {
        TextView timerName;
        TextView timerTime;
        public CardViewHolder(View itemView) {
            super(itemView);
            timerName = (TextView) itemView.findViewById(R.id.titleString);
            timerTime = (TextView) itemView.findViewById(R.id.timerString);

        }
    }
}

item_timer.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:layout_margin="5dp"
    android:orientation="vertical"
    app:cardCornerRadius="20dp"
    app:cardElevation="10dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/timerString"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:fontFamily="@font/space_grotesk"
            android:textSize="60sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/titleString"
            android:layout_width="165dp"
            android:layout_height="45dp"
            android:layout_marginTop="4dp"
            android:fontFamily="@font/space_grotesk"
            android:maxLength="35"
            android:maxLines="2"
            android:textAlignment="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/timerString"
            app:layout_constraintVertical_bias="0.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
vxl17
  • 1
  • 2

2 Answers2

0

You can give your Cardview an android:id:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idNameOfCardView"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_margin="5dp"
android:orientation="vertical"
app:cardCornerRadius="20dp"
app:cardElevation="10dp">

And then you can use this id in your Adapter in the onBindViewHolder Method, like the following (also make sure to declare a CardView in your CardViewHolder):

@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
    holder.idNameOfCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
              // Your Code here on one Click
        }
    });

    viewHolder.idNameOfCardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            // Your Code here on long click
            return false;
        }
    });
}

The double tap is a little bit more difficult, but you can use a GestureDetector, here you can read more about that: How to detect double tap

Ian
  • 21
  • 4
0

I would like to override your adapter a bit.

First of All you need the callback / delegate to activity either fragment.

public interface ClickListener {
        void onClick(Timer timer);

        void onDoubleClick(Timer timer);

        void onLongClicked(Timer timer);
}

For constructor

private final ClickListener clickListener;

public CardAdapter(ArrayList<Timer> data, ClickListener listener) {
        this.data = data;
        this.clickListener = listener;
}

And for onBindViewHolder you need just change a bit.

Timer timer = data.get(position);
holder.timerName.setText(timer.getName());
holder.timerTime.setText(timer.getTimeDisplay());

holder.itemView.setOnTouchListener(new View.OnTouchListener() {
        private final GestureDetector gestureDetector = new GestureDetector(holder.itemView.getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.d("Nrohpos", "onDoubleTap");
                clickListener.onDoubleClick(timer);

                return super.onDoubleTap(e);
            }

            @Override
            public boolean onSingleTapConfirmed(@NonNull MotionEvent e) {
                Log.d("Nrohpos", "onSingleTapUp");

                clickListener.onClick(timer);

                return super.onSingleTapConfirmed(e);
            }

            @Override
            public void onLongPress(@NonNull MotionEvent e) {
                Log.d("Nrohpos", "onLongPress");

                clickListener.onLongClicked(timer);
                super.onLongPress(e);
            }
        });

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });

Finally about Activity / Fragment

CardAdapter cardAdapter = new CardAdapter(timerArrayList, new CardAdapter.ClickListener() {
            @Override
            public void onClick(Timer timer) {
                    // your logic here
            }

            @Override
            public void onDoubleClick(Timer timer) {
                // your logic here
            }

            @Override
            public void onLongClicked(Timer timer) {
                    // your logic here
            }
        });
Nrohpos
  • 283
  • 2
  • 7