반응형
[안드로이드] 이미지 스와이프 (뷰페이저, ViewPager) Image Swipe
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com.example.kb4devimageslider; import android.app.Activity; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class CustomAdapter extends PagerAdapter{ Context context; int [] imageId = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5}; public CustomAdapter(Context context){ this .context = context; } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub LayoutInflater inflater = ((Activity)context).getLayoutInflater(); View viewItem = inflater.inflate(R.layout.image_item, container, false ); ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView); imageView.setImageResource(imageId[position]); TextView textView1 = (TextView) viewItem.findViewById(R.id.textView1); textView1.setText( "hi" ); ((ViewPager)container).addView(viewItem); return viewItem; } @Override public int getCount() { // TODO Auto-generated method stub return imageId.length; } @Override public boolean isViewFromObject(View view, Object object) { // TODO Auto-generated method stub return view == ((View)object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager) container).removeView((View) object); } } |
Now let us implement the MainActivity
- First we create a ViewPager object.
- Then we proceed towards creating an adapter and passing the context of current activity to it.
- After that, we add the adapter to ViewPager.
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 | package com.example.kb4devimageslider; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; public class MainActivity extends Activity { ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); PagerAdapter adapter = new CustomAdapter(MainActivity. this ); viewPager.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true ; } }
|
[출처 : http://kb4dev.com/article/android-image-slider-with-swipe-gesture ]
반응형
'IT > 안드로이드+JAVA' 카테고리의 다른 글
[안드로이드 스튜디오] 메모리 공간 늘리기 (0) | 2016.09.06 |
---|---|
[안드로이드] 커스텀 다이얼로그 예제 (0) | 2016.09.05 |
[안드로이드] EditText 모서리 둥글게 (0) | 2016.09.02 |
[안드로이드] 체크박스 박스 없애고 이미지 넣기 (0) | 2016.09.02 |
[안드로이드] 외부 글꼴 설정하기, 모든 액티비티에 글꼴 설정하기 (0) | 2016.08.30 |