- Home »
- 안드로이드(Android) »
- 안드로이드[Android] ViewPager(화면전환) -Touch
Windows 8 UI > Desgined By. Renadel Dapize
Unknown
On 2014년 7월 14일 월요일
뷰페이저(ViewPager)는
예전 쓰던 기능을 보면 ViewFliper, GalleryView, Horizontal ScrollView, 버튼 화면전환 등 많이 있지만,
쓰기 편한 방법이 ViewPager(터지로 화면 좌우전환효과)
1. MainActivity를 추가하시고
1
2
3
4
5
6
7
8
9
10
11
12
| public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); ImageAdapter adapter = new ImageAdapter( this ); viewPager.setAdapter(adapter); } } |
레이아웃
2.activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
| android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < android.support.v4.view.ViewPager android:id = "@+id/view_pager" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </ RelativeLayout > |
3.ViewPager에 해당하는 Adapter 클래스를 구현
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| public class ImageAdapter extends PagerAdapter { Context context; private int [] GalImages = new int [] { R.drawable.one, R.drawable.two, R.drawable.three }; ImageAdapter(Context context){ this .context=context; } @Override public int getCount() { return GalImages.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == ((ImageView) object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium); imageView.setPadding(padding, padding, padding, padding); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageView.setImageResource(GalImages[position]); ((ViewPager) container).addView(imageView, 0 ); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((ImageView) object); } }
하면 끝! |
블로그
Powered by Blogger.