服务器之家

服务器之家 > 正文

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

时间:2020-12-28 10:45     来源/作者:Android教程网

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

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部