IT/안드로이드+JAVA

[안드로이드] 이미지 스와이프 (뷰페이저, ViewPager)

안경 쓴 귀니 2016. 9. 2. 17:52
반응형





[안드로이드] 이미지 스와이프 (뷰페이저, 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

  1. First we create a ViewPager object.
  2. Then we proceed towards creating an adapter and passing the context of current activity to it.
  3. 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 ]

반응형