반응형
안드로이드 세로로된 ViewPager 입니다.
기본적으로 튜토리얼이나 이미지 리스트들 보여줄때 사용하는데 사용하는 것입니다.
틱톡(Tiktok)처럼 해볼까 하여 만들었습니다만 틱톡처럼 할수 없을거 같습니다. 기본적으로 스크롤 해서 넘길경우
라이프사이클이 대로 영상등을 제어 해야할것 같기 때문입니다. 그런식으로 할려면 Fragment형 ViewPager를
사용하시기 바랍니다. 한참전에 만들어 놓은 샘플소스라서 잘 기억이 나진 않지만 검색해보니
FragmentPagerAdapter 인거 같습니다.
파일은 ViewPager 를 상속받은 CustomViewPager 와 PageAdapter 를 상속받은 SampleAdapter
그리고 기본적인 Activity 가 있습니다.
1. ViewPagerActivity.java
public class ViewPagerActivity extends Activity { private CustomViewPager viewPager; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pager); SampleAdapter adapter = new SampleAdapter(this); viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(adapter ); } }
2. CustomViewPager.java
public class CustomViewPager extends ViewPager { public static final int HORIZONTAL = 0; public static final int VERTICAL = 1; private int mSwipeOrientation; public CustomViewPager(Context context) { super(context); mSwipeOrientation = HORIZONTAL; } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); setSwipeOrientation(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(mSwipeOrientation == VERTICAL ? swapXY(event) : event); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (mSwipeOrientation == VERTICAL) { boolean intercepted = super.onInterceptHoverEvent(swapXY(event)); swapXY(event); return intercepted; } return super.onInterceptTouchEvent(event); } private void setSwipeOrientation(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewPager); mSwipeOrientation = typedArray.getInteger(R.styleable.CustomViewPager_swipe_orientation, 0); typedArray.recycle(); initSwipeMethods(); } private void initSwipeMethods() { if (mSwipeOrientation == VERTICAL) { // The majority of the work is done over here setPageTransformer(true, new VerticalPageTransformer()); // The easiest way to get rid of the overscroll drawing that happens on the left and right setOverScrollMode(OVER_SCROLL_NEVER); } } private MotionEvent swapXY(MotionEvent event) { float width = getWidth(); float height = getHeight(); float newX = (event.getY() / height) * width; float newY = (event.getX() / width) * height; event.setLocation(newX, newY); return event; } private class VerticalPageTransformer implements ViewPager.PageTransformer { @Override public void transformPage(View page, float position) { if (position < -1) { // This page is way off-screen to the left page.setAlpha(0); } else if (position <= 1) { page.setAlpha(1); // Counteract the default slide transition page.setTranslationX(page.getWidth() * -position); // set Y position to swipe in from top float yPosition = position * page.getHeight(); page.setTranslationY(yPosition); } else { // This page is way off screen to the right page.setAlpha(0); } } } }
3. SampleAdapter.java
아래 drawableImgs 에 이미지 파일들을 적어주시면 되겠습니다.
public class SampleAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener{ Context context; private final int[] drawableImgs = new int[] { R.drawable.heena, R.drawable.joy, R.drawable.ierin, }; public SampleAdapter(Context context) { this.context = context; } @Override public int getCount() { return drawableImgs.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); Bitmap drawImg = BitmapFactory.decodeResource(context.getResources(), drawableImgs[position]); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; drawImg = Bitmap.createScaledBitmap(drawImg, width, height, false); imageView.setImageBitmap(drawImg); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP ); container.addView(imageView, 0); // drawImg.recycle(); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((ImageView) object); } @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { } @Override public void onPageScrollStateChanged(int i) { } }
Android Vertical ViewPager
반응형
'프로그램 > Android' 카테고리의 다른 글
BottomSheetBehavior 사용법 및 후기 (0) | 2022.02.16 |
---|---|
안드로이드 드래그앤 드롭(drag&drop) 사용법 (0) | 2020.07.03 |
안드로이드 레이아웃 PDF 변환 (0) | 2019.02.26 |
안드로이드 FCM 푸시(2.페이로드 및 테스트) (1) | 2019.01.03 |
안드로이드 FCM 푸시(1.프로젝트 셋팅 및 구현) (1) | 2019.01.03 |