欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 左右滑动+索引图标实现方法与代码

程序员文章站 2023-12-14 18:06:34
使用gallery和imageview实现android左右滑动+索引图标效果。 首先自定义gallery实现一次只能滑动一个页面 复制代码 代码如下: public...

使用gallery和imageview实现android左右滑动+索引图标效果。

android 左右滑动+索引图标实现方法与代码

android 左右滑动+索引图标实现方法与代码
首先自定义gallery实现一次只能滑动一个页面

复制代码 代码如下:

public class mgalleryview extends gallery{
public mgalleryview(context context, attributeset attrs) {
super(context, attrs);
}
//一次只能滑动一张图片注:一张图充满全屏
@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();
}
}

然后再主activity"mgalleryactivity"的xml中注册一下
复制代码 代码如下:

<?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"
>
<!--自定义的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的索引图片容器 -->
<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>

主activity的java代码
复制代码 代码如下:

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每次滑动都调用这个方法
gallery.setonitemselectedlistener(new onitemselectedlistener() {
@override
public void onitemselected(adapterview<?> arg0, view arg1,
int arg2, long arg3) {
//使用for循环,给所有的gallery索引添加图片
for (int i = 0; i < imageviews.length; i++) {
imageviews[i]
.setimageresource(r.drawable.alipay_n);
}
//为被选中的gallery的索引添加图片
imageviews[arg2].setimageresource(r.drawable.alipay_s);
}
@override
public void onnothingselected(adapterview<?> arg0) {
}
});
}
//设置左右滑动索引
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) {
//默认选择第一张
images[i].setimageresource(r.drawable.alipay_n);
} else {
images[i].setimageresource(r.drawable.alipay_s);
}
viewgroup.addview(images[i]);
}
return images;
}
}

gallery的适配器
复制代码 代码如下:

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;
}
}

上一篇:

下一篇: