首页 > 系统 > Android > 正文

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

2020-04-11 12:37:31
字体:
来源:转载
供稿:网友

使用Gallery和ImageView实现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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表