我有ViewPager
,我希望通过调用viewPager.setCurrentItem(index, true)
来平滑滚动。 ViewPager
由其中包含RecyclerView
的片段组成。
当用户在片段的标题上向右或向左滑动时,我调用setCurrentItem
,然后查看寻呼机将滚动到下一个项目。但问题是视图寻呼机没有(每次)平滑滚动到下一个片段。我尝试了一些我在这里找到的解决方案,但没有真正起作用。
奇怪的是有时候(大多数时候我从右到左)它会顺利滚动,但大多数情况下都不起作用。
编辑:
这是我的代码:
ViewPagerFragment:
...
private void setViews(){
header.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
public void onSwipeRight() {
swipeRight();
}
public void onSwipeLeft() {
swipeLeft();
}
});
}
private void swipeLeft(){
final CustomSwipeViewPager viewPager = (CustomSwipeViewPager) getActivity().findViewById(R.id.list_view_pager);
final int currentItem = viewPager.getCurrentItem();
if(currentItem < 4){
Thread sepThread = new Thread(new Runnable(){
public void run()
{
//Then, inside that thread, run a runnable on the ui thread.
//MyActivity.getContext() is a static function that returns the
//context of the activity. It's us.getContext()eful in a pinch.
getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
viewPager.setCurrentItem(currentItem + 1, true);
}
});
}
});
sepThread.start();
}
}
private void swipeRight(){
final CustomSwipeViewPager viewPager = (CustomSwipeViewPager) getActivity().findViewById(R.id.list_view_pager);
final int currentItem = viewPager.getCurrentItem();
if(currentItem > 0){
Thread sepThread = new Thread(new Runnable(){
public void run()
{
getActivity().runOnUiThread(new Runnable(){
@Override
public void run() {
viewPager.setCurrentItem(currentItem - 1, true);
}
});
}
});
sepThread.start();
}
}
...
自定义触控侦听器:
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
我希望这就是你所需要的。
答案 0 :(得分:1)
我发现为什么ViewPager
每次都不能顺利滚动。看起来ViewPager中单个Fragments
的创建时间太长,这就是为什么ViewPager每次都不能平滑滚动的原因。它可以平滑滚动,因为片段已经初始化,但情况并非总是如此。
似乎我必须更快地创建片段。