第三,实现自动滚动

由于我们还要手动滚动,所以自动滚动用单独一个进程来实现

   
   
   
   
  1. private void startAutoScroll() { 
  2.         new Thread() { 
  3.             @Override 
  4.             public void run() { 
  5.                 int count = 0
  6.                 while (mAutoScroll) { 
  7.                     count = 0
  8.                     while (count < 30) { 
  9.                         count++; 
  10.                         try { 
  11.                             Thread.sleep(100); 
  12.                         } catch (InterruptedException e) { 
  13.                             e.printStackTrace(); 
  14.                         } 
  15.                         if (mOnTouch) {// 用戶手动滑动时,停止自动滚动 
  16.                             count = 0
  17.                         } 
  18.                     } 
  19.                     mPosition++; 
  20.                     Message msg = mHandler.obtainMessage(SCROLL, mPosition, 0); 
  21.                     mHandler.sendMessage(msg); 
  22.                 } 
  23.             } 
  24.  
  25.         }.start(); 
  26.     } 
  27.  
  28.     private Handler mHandler = new Handler() { 
  29.  
  30.         @Override 
  31.         public void handleMessage(Message msg) { 
  32.             switch (msg.what) { 
  33.             case SCROLL: 
  34.                 mGallery.setSelection(msg.arg1); 
  35.                 break
  36.             } 
  37.         }  
  38.  
  39.    }; 

 

第四 实现手动滚动

手动滚动时,要停止自动滚动,监听galleryonTouch事件,DOWNmOnTouch置为true,UPmOnTouch置为false即可

   
   
   
   
  1. mGallery.setOnTouchListener(new OnTouchListener() { 
  2.  
  3.             @Override 
  4.             public boolean onTouch(View v, MotionEvent event) { 
  5.                 int action = event.getAction(); 
  6.                 if (action == MotionEvent.ACTION_DOWN) { 
  7.                     mOnTouch = true
  8.                 } else if (action == MotionEvent.ACTION_UP) { 
  9.                     mOnTouch = false
  10.                 } 
  11.                 return false
  12.             } 
  13.  
  14.         }); 

 

到现在我们已经可以自动滚动,手动滚动时自动滚动也会停止。

我们也许还需要加上dot提示图片滚动的位置

   
   
   
   
  1. LinearLayout layout = (LinearLayout) findViewById(R.id.dot); 
  2.         if (mDots == null) { 
  3.             mDots = new ImageView[ids.length]; 
  4.             for (int i = 0; i < ids.length; i++) { 
  5.                 if (mDots[i] == null
  6.                     mDots[i] = new ImageView(this); 
  7.  
  8.                 mDots[i].setBackgroundResource(R.drawable.banner_tab_unselected); 
  9.                 layout.addView(mDots[i], new LinearLayout.LayoutParams(mWidth 
  10.                         / ids.length + 1, LayoutParams.WRAP_CONTENT)); 
  11.             } 
  12.             mDots[0].setBackgroundResource(R.drawable.banner_tab_selected); 
  13.         } 

 

   
   
   
   
  1. mGallery.setOnItemSelectedListener(new OnItemSelectedListener() { 
  2.  
  3.             @Override 
  4.             public void onItemSelected(AdapterView arg0, View view, 
  5.                     int position, long arg3) { 
  6.                 mDotPosition = position % ids.length; 
  7.                 mDots[mDotPosition] 
  8.                         .setBackgroundResource(R.drawable.banner_tab_selected); 
  9.                 if (mDotPosition != mPreDotPosition) 
  10.                     mDots[mPreDotPosition] 
  11.                             .setBackgroundResource(R.drawable.banner_tab_unselected); 
  12.                 mPreDotPosition = mDotPosition; 
  13.             } 
  14.  
  15.             @Override 
  16.             public void onNothingSelected(AdapterView arg0) { 
  17.                                  
  18.             } 
  19.              
  20.         });