android 左右滑動+索引表徵圖實現方法與代碼

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.