setCurrentItem(count,true);
마지막에 true는 스무스하게 움직이게 하는거다
는 무슨.... 작동이 안된다.
그래서 ViewPager를 상속받아서 새로 만들었다.
package com.example.redcarpetwall;
import java.lang.reflect.Field;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
public class ImageSlidePager extends ViewPager{
public ImageSlidePager(Context context) {
super(context);
}
public ImageSlidePager(Context context, AttributeSet attr) {
super(context, attr);
setMyScroller();
}
private void setMyScroller()
{
try
{
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e)
{
e.printStackTrace();
}
}
public class MyScroller extends Scroller
{
public MyScroller(Context context)
{
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration)
{
super.startScroll(startX, startY, dx, dy, 1000 /*1 secs*/);
}
}
}
그러니 잘 작동한다.
이건 imageslide adapter
package com.example.redcarpetwall;
import android.R;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
// view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
// LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
//view.setBackgroundResource(imageArray[position]);
view.setImageResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
이건 xml
<com.example.redcarpetwall.ImageSlidePager
android:id="@+id/imageSlidePager1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.redcarpetwall.ImageSlidePager>
귀찮아서 그냥 다 복불 ㅎㅎ
나중에 시간 되면 정리해야겠다.
참고한 사이트
이미지 슬라이드 : http://androidtrainningcenter.blogspot.kr/2012/10/viewpager-example-in-android.html