2016-08-31 45 views

回答

6

請在這裏查看解決方案。 https://github.com/ritesh-bhavsar86/StockAutoScroll

首先創建可運行:

final int duration = 10; 
final int pixelsToMove = 30; 
private final Handler mHandler = new Handler(Looper.getMainLooper()); 
private final Runnable SCROLLING_RUNNABLE = new Runnable() { 

    @Override 
    public void run() { 
     rv_autoScroll.smoothScrollBy(pixelsToMove, 0); 
     mHandler.postDelayed(this, duration); 
    } 
}; 

然後setadapter()到recyclerView使用後下列:

rv_autoScroll.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 
      int lastItem = layoutManager.findLastCompletelyVisibleItemPosition(); 
      if(lastItem == layoutManager.getItemCount()-1){ 
       mHandler.removeCallbacks(SCROLLING_RUNNABLE); 
       Handler postHandler = new Handler(); 
       postHandler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         rv_autoScroll.setAdapter(null); 
         rv_autoScroll.setAdapter(madapter); 
         mHandler.postDelayed(SCROLLING_RUNNABLE, 2000); 
        } 
       }, 2000); 
      } 
     } 
    }); 
    mHandler.postDelayed(SCROLLING_RUNNABLE, 2000); 

rv_autoScroll是recyclerview

LayoutManager是其中的LayoutManager設置爲recyclerview