Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a method for double click , fix the problem of double click #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package devs.mulham.horizontalcalendar.adapter;

import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

import java.util.Calendar;
Expand Down Expand Up @@ -36,6 +40,7 @@ public abstract class HorizontalCalendarBaseAdapter<VH extends DateViewHolder, T
private final int cellWidth;
private CalendarItemStyle disabledItemStyle;


protected Calendar startDate;
protected int itemsCount;

Expand All @@ -61,6 +66,7 @@ public VH onCreateViewHolder(ViewGroup parent, int viewType) {
viewHolder.itemView.setOnClickListener(new MyOnClickListener(viewHolder));
viewHolder.itemView.setOnLongClickListener(new MyOnLongClickListener(viewHolder));


if (eventsPredicate != null) {
initEventsRecyclerView(viewHolder.eventsRecyclerView);
} else {
Expand Down Expand Up @@ -154,24 +160,59 @@ public void update(Calendar startDate, Calendar endDate, boolean notify) {
}
}



protected abstract int calculateItemsCount(Calendar startDate, Calendar endDate);


private class MyOnClickListener implements View.OnClickListener {
private int timeDoubleClick;
private Handler handler;
private long timeFirstClick;
private final RecyclerView.ViewHolder viewHolder;

MyOnClickListener(RecyclerView.ViewHolder viewHolder) {
this.viewHolder = viewHolder;
timeDoubleClick = ViewConfiguration.getDoubleTapTimeout();
timeFirstClick = 0L;
handler = new Handler(Looper.getMainLooper());
}

@Override
public void onClick(View v) {
int position = viewHolder.getAdapterPosition();
if (position == -1)
long now = System.currentTimeMillis();
final int position = viewHolder.getAdapterPosition();
if (position == -1){
return;
}else if (now - timeFirstClick < timeDoubleClick) {
handler.removeCallbacksAndMessages(null);
timeFirstClick = 0L;
DoubleClick(position);
}else {
timeFirstClick = now;
handler.postDelayed(new Runnable() {
@Override
public void run() {
SingleClick(position);
timeFirstClick = 0L;
}
}, timeDoubleClick);
}

}

public void SingleClick(int position){
Log.i("signle","click");
horizontalCalendar.getCalendarView().setSmoothScrollSpeed(HorizontalLayoutManager.SPEED_SLOW);
horizontalCalendar.centerCalendarToPosition(position);
}

public void DoubleClick(int position){
Log.i("double","click");
horizontalCalendar.getCalendarView().setSmoothScrollSpeed(HorizontalLayoutManager.SPEED_SLOW);
horizontalCalendar.centerCalendarToPosition(position);
}

}

private class MyOnLongClickListener implements View.OnLongClickListener {
Expand All @@ -195,3 +236,4 @@ public boolean onLongClick(View v) {
}
}
}