First, customize the gallery implementation to slide only one page at a time
The code is as follows:
public class Mgalleryview extends gallery{
Public Mgalleryview (context context, AttributeSet Attrs) {
Super (context, attrs);
}
You can only swipe one picture at a time note: A picture fills 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 in the XML of the main activity "mgalleryactivity"
The 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 Indexed picture 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 for the main activity
The 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 you swipe
Gallery.setonitemselectedlistener (New Onitemselectedlistener () {
@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
Add a picture to all gallery indexes using a for loop
for (int i = 0; i < imageviews.length; i++) {
Imageviews[i]
. Setimageresource (R.drawable.alipay_n);
}
Add a picture to the index of the selected gallery
Imageviews[arg2].setimageresource (r.drawable.alipay_s);
}
@Override
public void onnothingselected (adapterview<?> arg0) {
}
});
}
Set 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 default selection is the first one
Images[i].setimageresource (R.drawable.alipay_n);
} else {
Images[i].setimageresource (r.drawable.alipay_s);
}
Viewgroup.addview (Images[i]);
}
return images;
}
}
Adapter for Gallery
The 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;
}
}
Android left and right slide + index icon implementation method and code