Android : simple ViewPager
04/08/2012
FacebookTwitterDeliciousStumbleuponDiggMyspaceLinkedInZingmegovnlinkhay


Xin chào , hôm nay, tôi sẽ tạo một viewpager đơn giản chỉ để chứa hình ảnh. Bước 1, download android-support-v4 Bước 2, tạo main.xml Bước 3, tạo pagerAdapter Bước 4, setAdapter tại activity  
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
mail.xml
PHP Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/view_pager"/>
</LinearLayout>
PHP Code:
<?xml version="1. 0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
     android:id="@+id/img"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
        />
</LinearLayout>
đây là activity
PHP Code:
package com.papager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class PagerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyPager myViewPager = new MyPager();
        ViewPager mypager = (ViewPager)findViewById(R.id.view_pager);
        mypager.setAdapter(myViewPager);
        mypager.setCurrentItem(0);
    }
}
///////////////////////////////
Adapter
/////////////////////////////
PHP Code:
package com.papager;
import java.util.Collection;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class MyPager  extends PagerAdapter{
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        // TODO Auto-generated method stub
        ((ViewPager)arg0).removeView((View)arg2);
    }
    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;                                                                      // tổng số view
    }
    @Override
    public Object instantiateItem(View collection, int position) {
        // TODO Auto-generated method stub