我有一个 ScrollView ,它包含一个 ViewPager 和一个 RecyclerView ,我已经应用了计时器来自动滚动viewpager内容,我想要的是当我点击查看寻呼机时,它按下它不想执行自动滚动任务,为此我使用下面的代码。
viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
canScroll = false;
Log.e("canScroll", "ACTION_DOWN = " + canScroll);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
canScroll = true;
Log.e("canScroll", "ACTION_UP = " + canScroll);
}
return true;
}
});
public void pageSwitcher(int seconds) {
timer = new Timer(); // At this line a new Thread will be created
timer.scheduleAtFixedRate(new RemindTask(), 0, seconds * 1000); // delay
}
// this is an inner class...
class RemindTask extends TimerTask {
@Override
public void run() {
try {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Log.e("canScroll", "RemindTask = " + canScroll);
if (canScroll) {
if (currentPage < maxPage) {
viewPager.setCurrentItem(currentPage);
currentPage = currentPage + 1;
} else {
viewPager.setCurrentItem(currentPage);
currentPage = 0;
}
}
}
});
} catch (Exception e) {
Log.e("Unwanted", "RemindTask- NullPointerException");
}
}
}
我想要的是,当我水平滚动viewpager时,它想要滚动但是它不能正常工作并且当我点击viewpager然后垂直滚动它时ScrollView会垂直滚动但是我不能能够检测到 MotionEvent.ACTION_UP ,并且由于我无法设置 canScroll = true 来执行自动滚动viewpager内容并且此布局位于的 片段 即可。
所以请有人帮助我。
答案 0 :(得分:0)
使用下面的代码
public void pageSwitcher() {
final Handler handler=new Handler();
Runnable runnable=new Runnable() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Log.e("canScroll", "RemindTask = " + canScroll);
if (canScroll) {
if (currentPage < maxPage) {
viewPager.setCurrentItem(currentPage);
currentPage++;
} else {
viewPager.setCurrentItem(currentPage);
currentPage = 0;
}
}
}
});
handler.postDelayed(this,1000);
}
};
handler.postDelayed(runnable,1000);
}
onTouch侦听器与您的
相同viewPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
canScroll = false;
Log.e("canScroll", "ACTION_DOWN = " + canScroll);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
canScroll = true;
Log.e("canScroll", "ACTION_UP = " + canScroll);
}
return true;
}
});
在Redmi设备中测试 评论是否有任何查询