作者:montplus手工饼干 | 来源:互联网 | 2021-08-15 19:26
使用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中注册一下
代码如下:
android:layout_
android:layout_
android:orientation="vertical"
> android:id="@+id/gallery_id"
android:layout_
android:layout_
android:spacing="50dp"
android:layout_marginTop="20dp"/> android:id="@+id/linearlayout_id"
android:layout_
android:layout_
android:layout_marginTop="-20dp"
android:orientation="horizontal"
android:gravity="center_horizontal">
主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[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 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;
}
}