关于实现android左右滑动+索引图标效果的代码
来源:爱站网时间:2021-05-12编辑:网友分享
移动用户在浏览某个界面的时候应该都有碰到过可以左右滑动的图片,那么这样的效果是如何实现的呢?下面爱站技术频道小编将为大家分享关于实现android左右滑动+索引图标效果的代码,有需要的android开发者可以参考下。
使用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_height="match_parent"
android:orientation="vertical"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="50dp"
android:layout_marginTop="20dp"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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;
}
}
以上就是爱站技术频道小编为大家分享的关于实现android左右滑动+索引图标效果的代码,如果你在android开发中也需要运用到这个效果的话,就可以参照以上代码内容。