我正在开发一个基于卡片设计的具有多个计时器的应用程序,我需要添加三种可能的交互方式:
我不知道如何添加 onClickListener 以及添加到哪里。 这是我的代码:
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>
您可以给您的 Cardview 一个
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">
然后你可以在适配器中的
id
方法中使用这个onBindViewHolder
,如下所示(还要确保在你的CardView
中声明一个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;
}
});
}
双击有点困难,但你可以使用 GestureDetector,在这里你可以阅读更多相关信息:How to detect double tap
我想稍微覆盖一下你的适配器。
首先,您需要回调/委托来激活任一片段。
public interface ClickListener {
void onClick(Timer timer);
void onDoubleClick(Timer timer);
void onLongClicked(Timer timer);
}
对于构造函数
private final ClickListener clickListener;
public CardAdapter(ArrayList<Timer> data, ClickListener listener) {
this.data = data;
this.clickListener = listener;
}
对于onBindViewHolder你只需要改变一点。
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;
}
});
最后关于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
}
});