Android Slide left and right + index icon implementation method and code

Source: Internet
Author: User

Use Gallery and ImageView to achieve the android slide between left and right + index icon effect.


First, you can customize Gallery to slide only one page at a time.

Copy codeThe Code is as follows: public class MGalleryView extends Gallery {
Public MGalleryView (Context context, AttributeSet attrs ){
Super (context, attrs );
}
// Only one image can be swiped at a time. Note: one image is full screen.
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
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 ();
}
}

Then register it in the xml of the main Activity "MGalleryActivity ".Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
>
<! -- Custom Gallery -->
<Com. example. demo. MGalleryView
Android: id = "@ + id/gallery_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: spacing = "50dp"
Android: layout_marginTop = "20dp"/>
<! -- Gallery's index image container -->
<LinearLayout
Android: id = "@ + id/linearlayout_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "-20dp"
Android: orientation = "horizontal"
Android: gravity = "center_horizontal"> </LinearLayout>
</LinearLayout>

Java code of the main activityCopy codeThe Code is as follows: public class MGalleryActivity extends Activity {
Int [] data = new int [] {R. drawable. image1, R. drawable. image2,
R. drawable. image3, R. drawable. image4, R. drawable. image5,
R. drawable. image6 };
Private ImageView [] imageviews;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. galleryactvity );
SetView ();
}
Private void setView (){
MGalleryView gallery = (MGalleryView) findViewById (R. id. gallery_id );
LinearLayout viewgroup = (LinearLayout) findViewById (R. id. linearlayout_id );
Imageviews = setGalleryIndex (viewgroup );
Gallery. setAdapter (new MGalleryAdapter (this, data ));
// Gallery calls this method every time it slides
Gallery. setOnItemSelectedListener (new OnItemSelectedListener (){
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// Use the for loop to add images to all gallery Indexes
For (int I = 0; I <imageviews. length; I ++ ){
Imageviews [I]
. SetImageResource (R. drawable. alipay_n );
}
// Add an image for the selected gallery Index
Imageviews [arg2]. setImageResource (R. drawable. alipay_s );
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
}
});
}
// Set the Left and Right slide Index
Private ImageView [] setGalleryIndex (LinearLayout viewgroup ){
ImageView [] images = new ImageView [data. length];
For (int I = 0; I <images. length; I ++ ){
ImageView newimage = new ImageView (this );
Newimage. setLayoutParams (new LayoutParams (20, 10 ));
Newimage. setPadding (5, 0, 5, 0 );
Images [I] = newimage;
If (I = 0 ){
// The first image is selected by default.
Images [I]. setImageResource (R. drawable. alipay_n );
} Else {
Images [I]. setImageResource (R. drawable. alipay_s );
}
Viewgroup. addView (images [I]);
}
Return images;
}
}

Gallery AdapterCopy codeThe Code is as follows: public class MGalleryAdapter extends BaseAdapter {
Private int [] data;
Private Context mContext;
Public MGalleryAdapter (Context context, int [] data ){
This. data = data;
This. mContext = context;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return data. length;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return 0;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView image = new ImageView (mContext );
Image. setImageResource (data [position]);
Return image;
}
}

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.