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