英文:
Scroll RecyclerView with LongClickListener assigned to a ImageView
问题
我想在图像视图上设置setOnLongClickListener时,以每秒2或3个项目的速度滚动RecyclerView。我搜索了很多并尝试了倒计时计时器和处理程序来按特定时间滚动RecyclerView,但没有成功。有什么方法可以实现这个目标?
我尝试过以下代码:
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (minutesCurrentPosition >= 0 && minutesCurrentPosition < minutesArray.length) {
minutesCurrentPosition++;
if (minutesCurrentPosition == minutesArray.length) {
minutesCurrentPosition--;
}
if (minutesCurrentPosition < minutesArray.length) {
rvMinutes.scrollToPosition(minutesCurrentPosition);
}
}
}
};
ivMinutesUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
handler.postDelayed(runnable, 500);
return false;
}
});
ivMinutesUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
handler.postDelayed(runnable, 500);
return false;
}
});
ivMinutesUp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
// 我们只关心按钮释放的时候。
if (pEvent.getAction() == MotionEvent.ACTION_DOWN) {
// handler.postDelayed(runnable, 300);
}
switch (pEvent.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(runnable);
break;
}
pView.onTouchEvent(pEvent);
return false;
}
});
请注意,上述代码中可能存在重复的部分。如果需要,可以根据实际情况删除其中一个ivMinutesUp.setOnLongClickListener
。
英文:
I want to scroll RecyclerView with 2 or 3 item/sec when setOnLongClickListener on image view. I searched a lot and tried a countdown timer and handler to scroll RecyclerView by specific time. but without any success. any IDEA how to achieve this.
I tried this :
private Runnable runnable = new Runnable() {
@Override
public void run() {
if (minutesCurrentPosition >= 0 && minutesCurrentPosition < minutesArray.length) {
minutesCurrentPosition++;
if (minutesCurrentPosition == minutesArray.length) {
minutesCurrentPosition--;
}
if (minutesCurrentPosition < minutesArray.length) {
rvMinutes.scrollToPosition(minutesCurrentPosition);
}
}
}
};
ivMinutesUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
handler.postDelayed(runnable,500);
return false;
}
});
ivMinutesUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
handler.postDelayed(runnable,500);
return false;
}
});
ivMinutesUp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
// We're only interested in when the button is released.
if (pEvent.getAction() == MotionEvent.ACTION_DOWN) {
// handler.postDelayed(runnable,300);
}
switch (pEvent.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(runnable);
break;
}
pView.onTouchEvent(pEvent);
return false;
}
});
答案1
得分: 0
这是您提供的代码的翻译:
private void hoursGoUpFast() {
isButtonReleased = false;
if (thread != null) {
thread.interrupt();
thread = null;
}
thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < hoursArray.length; i++) {
if (isButtonReleased) {
break;
} else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
nextHour();
}
}
}
});
thread.start();
}
private void hoursGoDownFast() {
isButtonReleased = false;
if (thread != null) {
thread.interrupt();
thread = null;
}
thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < hoursArray.length; i++) {
if (isButtonReleased) {
break;
} else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
previousHour();
}
}
}
});
thread.start();
}
on click:
// 小时长按
ivHoursUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
hoursGoUpFast();
// handler.postDelayed(runnable, 500);
return false;
}
});
ivHoursDown.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
hoursGoDownFast();
// handler.postDelayed(runnable, 500);
return false;
}
});
注意:我只翻译了您提供的代码部分,没有包括任何其他信息。如果您需要更多翻译或有其他问题,请随时提出。
英文:
I used this:
private void hoursGoUpFast() {
isButtonReleased = false;
if (thread != null) {
thread.interrupt();
thread = null;
}
thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < hoursArray.length; i++) {
if (isButtonReleased) {
break;
} else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
nextHour();
}
}
}
});
thread.start();
}
private void hoursGoDownFast() {
isButtonReleased = false;
if (thread != null) {
thread.interrupt();
thread = null;
}
thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < hoursArray.length; i++) {
if (isButtonReleased) {
break;
} else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
previousHour();
}
}
}
});
thread.start();
}
on click :
// hours long click
ivHoursUp.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
hoursGoUpFast();
// handler.postDelayed(runnable, 500);
return false;
}
});
ivHoursDown.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
hoursGoDownFast();
// handler.postDelayed(runnable, 500);
return false;
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论