Gallery implementation of quick drag only one page solution, gallery one page

Source: Internet
Author: User

Gallery implementation of quick drag only one page solution, gallery one page
Zookeeper

Gallery is rarely used this year. This article provides a method to slide only to the next page at a time (including fast sliding ).


Import android. content. Context;
Import android. util. AttributeSet;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. widget. Gallery;

Public class RecommendGallery extends Gallery {

Public RecommendGallery (Context context ){
Super (context );
}

Public RecommendGallery (Context context, AttributeSet attrs ){
Super (context, attrs );
}

@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
// TODO Auto-generated method stub
Int kEvent;
If (isScrollingLeft (e1, e2) {// Check if scrolling left
KEvent = KeyEvent. KEYCODE_DPAD_LEFT;
}
Else {// Otherwise scrolling right
KEvent = KeyEvent. KEYCODE_DPAD_RIGHT;
}
OnKeyDown (kEvent, null );
Return true;

}
Private boolean isScrollingLeft (MotionEvent e1, MotionEvent e2 ){
Return e2.getX ()> e1.getX ();
}
}


How does android Gallery achieve simultaneous sliding between the left and right

It is not feasible to simply nest ScrollView in Gallery, and horizontal sliding and vertical sliding events will conflict.
You can either rewrite the gallery by yourself, embed the horizontal slide event on the scrollView, or rewrite the ScrollView to process the Vertical Slide event in the gallery.

Forget to mention that nested gallery in the ScrollView is acceptable, but it is also limited to a small scope. For example, you can slide the display of four or five images on the top of a lot of content.

Android adds the navigation bar view under the Gallery. When you drag the Gallery, the navigation bar displays the progress based on the position of the current image in the Gallery.

Gallery is a ListView-like view that enables switching between images by dragging left and right slowly.

Package com. test;

Import java. util. ArrayList;
Import java. util. List;

Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. OS. Bundle;
Import android. text. Layout;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Import android. widget. LinearLayout. LayoutParams;

Public class SpinnerActivity extends Activity {
Private Gallery gallery;
Private List <Bitmap> list = new ArrayList <Bitmap> ();;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Gallery = (Gallery) findViewById (R. id. images );
Init ();
Gallery. setAdapter (new MyAdapter ());

}

Private void init (){
// Obtain the image object from the image factory and add it to the set
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));
List. add (BitmapFactory. decodeResource (getResources (), R. drawable. icon ));

}

Class MyAdapter extends BaseAdapter... the remaining full text>

Related Article

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.