Gallery automatic cyclic scrolling and smooth switching of manual scrolling

Source: Internet
Author: User

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

  1. Public class mygallery extends gallery {
  2. Public mygallery (context, attributeset attrs ){
  3. Super (context, attrs );
  4. // Todo auto-generated constructor stub
  5. }
  6. Private Boolean isscrollingleft (motionevent E1, motionevent E2 ){
  7. Return e2.getx ()> e1.getx ();
  8. }
  9. @ Override
  10. Public Boolean onfling (motionevent E1, motionevent E2, float velocityx,
  11. Float velocityy ){
  12. Int keycode;
  13. If (isscrollingleft (E1, E2 )){
  14. Keycode = keyevent. keycode_dpad_left;
  15. } Else {
  16. Keycode = keyevent. keycode_dpad_right;
  17. }
  18. Onkeydown (keycode, null );
  19. Return true;
  20. }
  21. }

@ 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

  1. Public class galleryadapter extends baseadapter {
  2. Private layoutinflater minflater;
  3. Private context mcontext;
  4. Private int width;
  5. Private int count;
  6. Private int [] mImageIds;
  7. Public GalleryAdapter (Context context, int [] ids ){
  8. MContext = context;
  9. MImageIds = ids;
  10. MInflater = LayoutInflater. from (mContext );
  11. DisplayMetrics dm = mContext. getApplicationContext (). getResources ()
  12. . GetDisplayMetrics ();
  13. Width = dm. widthPixels;
  14. Count = mImageIds. length;
  15. }
  16. @ Override
  17. Public int getCount (){
  18. Return Integer. MAX_VALUE; // used for cyclic scrolling
  19. }
  20. @ Override
  21. Public Object getItem (int position ){
  22. Return position;
  23. }
  24. @ Override
  25. Public long getitemid (INT position ){
  26. Return position;
  27. }
  28. @ Override
  29. Public View getview (INT position, view convertview, viewgroup parent ){
  30. Position = position % count;
  31. If (convertView = null ){
  32. ConvertView = mInflater. inflate (R. layout. gallery_item, null );
  33. }
  34. ImageView v = (ImageView) convertView. findViewById (R. id. img );
  35. V. setLayoutParams (new Gallery. LayoutParams (width, 200 ));
  36. V. setScaleType (ImageView. ScaleType. FIT_XY );
  37. V. setBackgroundResource (mImageIds [position]);
  38. Return v;
  39. }
  40. }


Third, implement automatic scrolling

Because we need to manually scroll, automatic scrolling is implemented using a separate process.

[HTML]
View plaincopy

  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) {// when the slider is manually swiped, stop auto-scroll
  16. Count = 0;
  17. }
  18. }
  19. MPosition ++;
  20. Message msg = mHandler. obtainMessage (SCROLL, mPosition, 0 );
  21. Mhandler. sendmessage (MSG );
  22. }
  23. }
  24. }. Start ();
  25. }
  26. Private handler mhandler = new handler (){
  27. @ Override
  28. Public void handlemessage (Message MSG ){
  29. Switch (msg. what ){
  30. Case SCROLL:
  31. MGallery. setSelection (msg. arg1 );
  32. Break;
  33. }
  34. }
  35. };


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

  1. MGallery. setOnTouchListener (new OnTouchListener (){
  2. @ Override
  3. Public boolean onTouch (View v, MotionEvent event ){
  4. Int action = event. getAction ();
  5. If (action = MotionEvent. ACTION_DOWN ){
  6. MOnTouch = true;
  7. } Else if (action = MotionEvent. ACTION_UP ){
  8. MOnTouch = false;
  9. }
  10. Return false;
  11. }
  12. });

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

  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. MDots [I]. setBackgroundResource (R. drawable. banner_tab_unselected );
  8. Layout. addView (mDots [I], new LinearLayout. LayoutParams (mWidth
  9. /Ids. length + 1, LayoutParams. WRAP_CONTENT ));
  10. }
  11. MDots [0]. setBackgroundResource (R. drawable. banner_tab_selected );
  12. }


[Java]
View plaincopy

  1. MGallery. setOnItemSelectedListener (new OnItemSelectedListener (){
  2. @ Override
  3. Public void onItemSelected (AdapterView <?> Arg0, View view,
  4. Int position, long arg3 ){
  5. MDotPosition = position % ids. length;
  6. MDots [mDotPosition]
  7. . SetBackgroundResource (R. drawable. banner_tab_selected );
  8. If (mDotPosition! = MPreDotPosition)
  9. MDots [mPreDotPosition]
  10. . SetBackgroundResource (R. drawable. banner_tab_unselected );
  11. MPreDotPosition = mDotPosition;
  12. }
  13. @ Override
  14. Public void onnothingselected (adapterview <?> Arg0 ){
  15. }
  16. });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.