IT/Android

View Pager

바바옄 2015. 6. 25. 16:55
반응형

1. 기본 View Page 구현

 

MainActivity.java

 

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
package com.ktds.myviewpager;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
 
 
public class MainActivity extends FragmentActivity implements View.OnClickListener{
 
 
    private final int MAX_FRAGMENT = 3;
    private final int FRAGMENT_ITEM_1 = 0;
    private final int FRAGMENT_ITEM_2 = 1;
    private final int FRAGMENT_ITEM_3 = 2;
 
    private Button firstBtn, secondBtn, thirdBtn;
    private ViewPager pager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        firstBtn = (Button) findViewById(R.id.firstBtn);
        // this : View.OnClickListener
        firstBtn.setOnClickListener(this);
        secondBtn = (Button) findViewById(R.id.secondBtn);
        secondBtn.setOnClickListener(this);
        thirdBtn = (Button) findViewById(R.id.thirdBtn);
        thirdBtn.setOnClickListener(this);
 
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        // 첫번째 fragment를 제일 먼저 보여 주겠다.
        pager.setCurrentItem(FRAGMENT_ITEM_1);
    }
 
    private class PagerAdapter extends FragmentPagerAdapter{
 
        // 생성자 필수
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }
 
        @Override
        public Fragment getItem(int i) {
 
            // 필요한 fragment 가져오기
            if(i == FRAGMENT_ITEM_1){
                return new FragmentPager1();
            }
            else if(i == FRAGMENT_ITEM_2){
                return new FragmentPager2();
            }
            else if(i == FRAGMENT_ITEM_3){
                return new FragmentPager3();
            }
            return null;
        }
 
        @Override
        public int getCount() {
            // pager가 보여줘야 하는 fragment 갯수
            return MAX_FRAGMENT;
        }
    }
 
 
    @Override
    public void onClick(View v) {
 
        // 어떤 button을 클릭했는지
        if(v.getId() == R.id.firstBtn){
            pager.setCurrentItem(FRAGMENT_ITEM_1);
        }
        else if(v.getId() == R.id.secondBtn){
            pager.setCurrentItem(FRAGMENT_ITEM_2);
        }
        else if(v.getId() == R.id.thirdBtn){
            pager.setCurrentItem(FRAGMENT_ITEM_3);
        }
    }
}
 
cs

 

 

activity_main.xml

 

 

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
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal|center_vertical">
 
        <Button
            android:id="@+id/firstBtn"
            android:text="첫번째"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:id="@+id/secondBtn"
            android:text="두번째"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:id="@+id/thirdBtn"
            android:text="세번째"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
 
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:requiresFadingEdge="horizontal"/>
 
</LinearLayout>
 
cs

 

 

FragmentPager1.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ktds.myviewpager;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
 
public class FragmentPager1 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pager_1, container, false);
    }
}
 
cs

 

 

fragment_pager_1.xml

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="첫번째 페이지" />
 
</LinearLayout>
 
cs

 

 

FragmentPager2.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.ktds.myviewpager;
 
 
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
 
public class FragmentPager2 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pager_2, container, false);
    }
 
 
}
 
cs

 

 

fragment_pager_2.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="두 번째 페이지" />
 
</LinearLayout>
cs

 

 

FragmentPager3.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ktds.myviewpager;
 
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class FragmentPager3 extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pager_3, container, false);
    }
}
 
cs

 

fragment_pager_3.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="세 번째 페이지" />
 
</LinearLayout>
cs

 

 

 

          

 

 

 

2.  외부 라이브러리를 사용해 View Pager 구현

 

dependency 추가 하기(외부 라이브러리 추가)

 

 

 

 

 

dependency 추가를 위한 Rebuild

 

 

 

 

MainActivity.java

 

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
package com.ktds.myviewpager;
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.astuetz.PagerSlidingTabStrip;
 
public class MainActivity extends FragmentActivity{
 
    private final int MAX_FRAGMENT = 5;
    private final int FRAGMENT_ITEM_1 = 0;
    private final int FRAGMENT_ITEM_2 = 1;
    private final int FRAGMENT_ITEM_3 = 2;
    private final int FRAGMENT_ITEM_4 = 3;
    private final int FRAGMENT_ITEM_5 = 4;
 
    private ViewPager pager;
    private PagerSlidingTabStrip tabs;
 
    private String[] pageTitle = {"Page 1","Page 2","Page 3","Page 4","Page 5"};
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        // 첫번째 fragment를 제일 먼저 보여 주겠다.
        pager.setCurrentItem(FRAGMENT_ITEM_1);
 
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setViewPager(pager);
    }
 
 
    private class PagerAdapter extends FragmentPagerAdapter{
 
        // 생성자 필수
        public PagerAdapter(FragmentManager fm) {
 
            super(fm);
        }
 
        @Override
        public CharSequence getPageTitle(int position) {
 
            return pageTitle[position];
        }
 
        @Override
        public Fragment getItem(int i) {
 
            // 필요한 fragment 가져오기
            if(i == FRAGMENT_ITEM_1){
                return new FragmentPager1();
            }
            else if(i == FRAGMENT_ITEM_2){
                return new FragmentPager2();
            }
            else if(i == FRAGMENT_ITEM_3){
                return new FragmentPager3();
            }
            else if(i == FRAGMENT_ITEM_4){
                return new FragmentPager4();
            }
            else if(i == FRAGMENT_ITEM_5){
                return new FragmentPager5();
            }
            return null;
        }
 
        @Override
        public int getCount() {
            // pager가 보여줘야 하는 fragment 갯수
            return MAX_FRAGMENT;
        }
    }
}
 
cs

 

 

activity_main.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="48dip"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:requiresFadingEdge="horizontal"/>
 
</LinearLayout>
 
cs

 

 

 

FragmentPager1.java, fragment_pager_1.xml

FragmentPager2.java, fragment_pager_2.xml

FragmentPager3.java, fragment_pager_3.xml

FragmentPager4.java, fragment_pager_4.xml

FragmentPager5.java, fragment_pager_5.xml 는 1번 기본 View Page 구현 소스와 같음.

 

 

 

        

 

 

반응형

'IT > Android' 카테고리의 다른 글

Fragment 전환  (0) 2015.06.25
Fragment Data 전달  (0) 2015.06.25
Networking - API 요청하기  (0) 2015.06.24
Networking - 웹으로 요청하기  (0) 2015.06.24
Thead & Runnable  (0) 2015.06.24