2015-11-01 8 views
0

мне нужно сделать некоторое волоча подобное действие на мой RecyclerView и когда я достигаю верхний или нижний конец RecyclerView Я хочу, чтобы прокрутить его гладко ...ReyclerView - программно прокручивать с заданным временем

Я делаю это как следующий: я отправляю эту runnable для прокрутки, пока пользователь не будет достаточно близко к началу/концу RecyclerView.

mScroll = new Runnable() { 
     @Override 
     public void run() { 
      rvImages.smoothScrollBy(0, mScrollDist.get()); 
      mHandler.postDelayed(mScroll, 100); 
     } 
    }; 

Проблема, мне нужно определить, что скроллинг желаемое расстояние должно быть сделано в 100мс, так что общая скроллинг гладкая.

Как я могу это достичь?

ответ

0

Использования пользовательского LinearLayoutManager

 public class MyCustomLayoutManager extends LinearLayoutManager { 
      //We need mContext to create our LinearSmoothScroller 
      private Context mContext; 

      public MyCustomLayoutManager(Context context) { 
       super(context); 
       mContext = context; 
      } 

      //Override this method? Check. 
      @Override 
      public void smoothScrollToPosition(RecyclerView recyclerView, 
       RecyclerView.State state, int position) { 

       //Create your RecyclerView.SmoothScroller instance? Check. 
       LinearSmoothScroller smoothScroller = 
        new LinearSmoothScroller(mContext) { 

        //Automatically implements this method on instantiation. 
        @Override 
    public PointF computeScrollVectorForPosition(int targetPosition) { 
     //What is PointF? A class that just holds two float coordinates. 
     //Accepts a (x , y) 
     //for y: use -1 for up direction, 1 for down direction. 
     //for x (did not test): use -1 for left direction, 1 for right 
     //direction. 
     //We let our custom LinearLayoutManager calculate PointF for us 
     return MyCustomLayoutManager.this.computeScrollVectorForPosition 
      (targetPosition); 
    } 
}; 

       //Docs do not tell us anything about this, 
       //but we need to set the position we want to scroll to. 
       smoothScroller.setTargetPosition(position); 

       //Call startSmoothScroll(SmoothScroller)? Check. 
       startSmoothScroll(smoothScroller); 
      } 
     } 

Настройка скорость приборов типа СПИРАЛЬНОЙ

//Make an instance variable at the top of you custom LayoutManager 
private static final float MILLISECONDS_PER_INCH = 50f; 
    . 
    . 
    . 
    LinearSmoothScroller smoothScroller = 
     new LinearSmoothScroller(mContext) { 
     . 
     . 
     . 
     //The holy grail of smooth scrolling 
     //returns the milliseconds it takes to scroll one pixel. 
     @Override 
     protected float calculateSpeedPerPixel 
      (DisplayMetrics displayMetrics) { 
      return MILLISECONDS_PER_INCH/displayMetrics.densityDpi; 
     } 
    }; 

В деятельности:

mLayoutManager = new MyCustomLayoutManager(getActivity()); 
mRecyclerView.setLayoutManager(mLayoutManager); 
mRecyclerView.smoothScrollToPosition(position); 

source code

Смежные вопросы