-3

setCurrentItem()関数を使用せずに自動ビューページスクロールを実装したいと思います。なぜなら、setcurrentitem()をhandler.postdelayed()で使用すると、次の項目を設定した後も保持されるからです。私はrecyclerviewのような滑らかなスクロールが欲しい。 viewpagerでそれを達成することは可能でしょうか?ハンドラなしのビューページ自動スクロールpostdelayedとsetcurrentitem

答えて

1

あなたはViewPagerを使用することができます。最高のカルーセルのようなrecylerviewを使用しています。しかし、あなたはカスタムLayoutManagerで最初

がRecyclerview

CustomLayoutManager layoutManager = new CustomLayoutManager(getActivity()); 
layoutManager.setSpeed(1500f); 
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 

を作成し、カルーセルのようなスピードを遅くする必要があるその後、あなたの活動で、その後recylcerviewスクロールの速度

public class CustomLayoutManager extends LinearLayoutManager { 
    private float MILLISECONDS_PER_INCH = 1100f; 
    private Context mContext; 

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

    @Override 
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { 

     LinearSmoothScroller smoothScroller = new LinearSmoothScroller(mContext) { 
        @Override 
        public PointF computeScrollVectorForPosition(int targetPosition) { 
         return CustomLayoutManager.this.computeScrollVectorForPosition(targetPosition); 
        } 

        @Override 
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { 
         return MILLISECONDS_PER_INCH/displayMetrics.densityDpi; 
        } 
       }; 

     smoothScroller.setTargetPosition(position); 
     startSmoothScroll(smoothScroller); 
    } 

    public void setSpeed(float speed){ 
     this.MILLISECONDS_PER_INCH = speed; 
    } 
} 

を変更するCustomLayoutManagerを作成しますか、フラグメント、ランナブルを作成する

Runnable runNews = new Runnable() { 
     @Override 
     public void run() { 
      try { 
       top_new_rview.smoothScrollToPosition(++counter); 
       handler.postDelayed(this, replay); 
      } catch (Exception e) { 
       handler.removeCallbacks(runNews); 
       onError(getActivity(), e); 
      } 
     } 
    }; 
関連する問題