From http://blog.csdn.net/lenghun00/article/details/7635374
@ Gallery when used with dot, if it is placed in relativelayout, the manual slide will rebound. Other layout problems are not fixed yet.
First, inherit the gallery and rewrite the onfling function to remove the rolling inertia of the gallery.
[Java]
View plaincopy
- Public class mygallery extends gallery {
- Public mygallery (context, attributeset attrs ){
- Super (context, attrs );
- // Todo auto-generated constructor stub
- }
- Private Boolean isscrollingleft (motionevent E1, motionevent E2 ){
- Return e2.getx ()> e1.getx ();
- }
- @ Override
- Public Boolean onfling (motionevent E1, motionevent E2, float velocityx,
- Float velocityy ){
- Int keycode;
- If (isscrollingleft (E1, E2 )){
- Keycode = keyevent. keycode_dpad_left;
- } Else {
- Keycode = keyevent. keycode_dpad_right;
- }
- Onkeydown (keycode, null );
- Return true;
- }
- }
@ Note onfling: directly returning false can achieve similar effects, but the image will be switched only when the sliding distance is great, and the user experience is poor.
Step 2: Construct the adapter
To smoothly implement cyclic scrolling, you can let getcount return a large value, so gallery thinks there are multiple items and the switching animation between items is smooth.
[HTML]
View plaincopy
- Public class galleryadapter extends baseadapter {
- Private layoutinflater minflater;
- Private context mcontext;
- Private int width;
- Private int count;
- Private int [] mImageIds;
- Public GalleryAdapter (Context context, int [] ids ){
- MContext = context;
- MImageIds = ids;
- MInflater = LayoutInflater. from (mContext );
- DisplayMetrics dm = mContext. getApplicationContext (). getResources ()
- . GetDisplayMetrics ();
- Width = dm. widthPixels;
- Count = mImageIds. length;
- }
- @ Override
- Public int getCount (){
- Return Integer. MAX_VALUE; // used for cyclic scrolling
- }
- @ Override
- Public Object getItem (int position ){
- Return position;
- }
- @ Override
- Public long getitemid (INT position ){
- Return position;
- }
- @ Override
- Public View getview (INT position, view convertview, viewgroup parent ){
- Position = position % count;
- If (convertView = null ){
- ConvertView = mInflater. inflate (R. layout. gallery_item, null );
- }
- ImageView v = (ImageView) convertView. findViewById (R. id. img );
- V. setLayoutParams (new Gallery. LayoutParams (width, 200 ));
- V. setScaleType (ImageView. ScaleType. FIT_XY );
- V. setBackgroundResource (mImageIds [position]);
- Return v;
- }
- }
Third, implement automatic scrolling
Because we need to manually scroll, automatic scrolling is implemented using a separate process.
[HTML]
View plaincopy
- Private void startAutoScroll (){
- New Thread (){
- @ Override
- Public void run (){
- Int count = 0;
- While (mAutoScroll ){
- Count = 0;
- While (count <30 ){
- Count ++;
- Try {
- Thread. Sleep (100 );
- } Catch (InterruptedException e ){
- E. printStackTrace ();
- }
- If (mOnTouch) {// when the slider is manually swiped, stop auto-scroll
- Count = 0;
- }
- }
- MPosition ++;
- Message msg = mHandler. obtainMessage (SCROLL, mPosition, 0 );
- Mhandler. sendmessage (MSG );
- }
- }
- }. Start ();
- }
- Private handler mhandler = new handler (){
- @ Override
- Public void handlemessage (Message MSG ){
- Switch (msg. what ){
- Case SCROLL:
- MGallery. setSelection (msg. arg1 );
- Break;
- }
- }
- };
Fourth, manual scrolling
When you manually scroll, you must stop automatic scrolling and listen to the ontouch event of gallery. If you go down, set montouch to true, and set montouch to false when you go up.
[Java]
View plaincopy
- MGallery. setOnTouchListener (new OnTouchListener (){
- @ Override
- Public boolean onTouch (View v, MotionEvent event ){
- Int action = event. getAction ();
- If (action = MotionEvent. ACTION_DOWN ){
- MOnTouch = true;
- } Else if (action = MotionEvent. ACTION_UP ){
- MOnTouch = false;
- }
- Return false;
- }
- });
Now we can automatically scroll, and the automatic Scroll will also stop when you manually scroll.
We may also need to add a dot to indicate the image scroll position.
[Java]
View plaincopy
- LinearLayout layout = (LinearLayout) findViewById (R. id. dot );
- If (mDots = null ){
- MDots = new ImageView [ids. length];
- For (int I = 0; I <ids. length; I ++ ){
- If (mDots [I] = null)
- MDots [I] = new ImageView (this );
- MDots [I]. setBackgroundResource (R. drawable. banner_tab_unselected );
- Layout. addView (mDots [I], new LinearLayout. LayoutParams (mWidth
- /Ids. length + 1, LayoutParams. WRAP_CONTENT ));
- }
- MDots [0]. setBackgroundResource (R. drawable. banner_tab_selected );
- }
[Java]
View plaincopy
- MGallery. setOnItemSelectedListener (new OnItemSelectedListener (){
- @ Override
- Public void onItemSelected (AdapterView <?> Arg0, View view,
- Int position, long arg3 ){
- MDotPosition = position % ids. length;
- MDots [mDotPosition]
- . SetBackgroundResource (R. drawable. banner_tab_selected );
- If (mDotPosition! = MPreDotPosition)
- MDots [mPreDotPosition]
- . SetBackgroundResource (R. drawable. banner_tab_unselected );
- MPreDotPosition = mDotPosition;
- }
- @ Override
- Public void onnothingselected (adapterview <?> Arg0 ){
- }
- });