1. onColorClick과 switch()를 이용한 구성


그 다음엔 switch 를 이용하여 fragment간 이동을 가능하게 하는 onclicklistener 를 구현해 보겠습니다.

첫 번째 방법입니다.


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
    Fragment frag_red;
    Fragment frag_blue;
    Fragment frag_green;
    Fragment frag_yellow;
    //    Fragment fragment = new Fragment();
    //color  0 red 1 blue 2green 3 yellow
    @Override
    public void onColorClick(int color) {
        Fragment frag = null;
        switch (color) {
            case 0:
                frag_red = getSupportFragmentManager().findFragmentByTag("red");
                frag = frag_red;
                if(frag_red==null){
                    frag_red=new RedFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag_red, "red").commit();
                }
                break;
            case 1:
                frag_blue = getSupportFragmentManager().findFragmentByTag("blue");
                frag = frag_blue;
                if(frag_blue==null){
                    frag_blue=new BlueFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag_blue, "blue").commit();
                }
                break;
            case 2:
                frag_green = getSupportFragmentManager().findFragmentByTag("green");
                frag = frag_green;
                if(frag_green==null){
                    frag_green=new GreenFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag_green, "green").commit();
                }
                break;
            case 3:
                frag = frag_yellow;
                frag_yellow = getSupportFragmentManager().findFragmentByTag("yellow");
                if(frag_yellow==null){
                    frag_yellow=new YellowFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_ex, frag_yellow, "yellow").commit();
                }
                break;
        }
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_ex, frag)
                .addToBackStack(null)
                .commit();
    }
 
cs


이렇게 구현하면 바로 commit()를 해줄 수 있는 장점이 있습니다. 그러나 언급되지 않은 getSupportFragmentManager()가 사용되었습니다.

그것은 여기서 가져오면 됩니다. 사용될 프래그먼트들을 태그를 통해 가져오고 그 값을 통해 if문과 case문을 결합하여 beginTransaction() 을 호출합니다. 


2. 준비물

1
2
3
4
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
cs


여기서 getSupportFragmentManager()가 사용되게 됩니다. 이 부분을 잊으면 진행에 문제가 되니 주의해야 합니다. Fragment를 이용하기 위해선 Activity, Manager, Transaction 모두를 다 불러와야 합니다.

다음엔 다른 방법을 알아보겠습니다.

'Java' 카테고리의 다른 글

[android] 구글맵 3  (0) 2018.02.03
[android] 구글맵 2  (0) 2018.02.03
[android] 구글맵 1  (0) 2018.02.03
[android] Fragment 만들기 2-2  (0) 2018.02.03
[android] Fragment 만들기1  (0) 2018.02.03

1.준비물

프래그먼트를 만들때는 먼저 main activity 를 설정해야 합니다. 

가져와야 할 모듈은 다음과 같습니다.


1
2
3
4
5
6
7
8
9
10
11
12
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.example.keepair.myapplication.adapter.MyPagerAdapter;
 
cs
dd

이 다음엔 main activity 를 설정할텐데 이렇게 하면 됩니다.


1
public class MainActivity extends FragmentActivity {}
cs


이렇게 extends 로 FragmentActivity를 써줘야 합니다.

그런 다음에 그 안에 들어갈 코드들을 작성하면 됩니다.

우선 이미지뷰를 가져옵니다.

2. 뷰 불러오기


1
2
3
4
5
6
7
    private ImageView btn_red;
    private ImageView btn_blue;
    private ImageView btn_yellow;
    private ImageView btn_green;
 
    private ViewPager vp_pager;
 
cs


그 다음에는 이미지 뷰를 어떻게 가져올지 결정해야 합니다.

case를 쓰는 방법과 onClickListener를 쓰는 방법이 있습니다.


onColorClick 을 쓰는 방법 - if 문과 case 문

1
2
3
4
5
6
7
8
9
    @Override
    public void onColorClick(int color) {
        switch (color) {
            case 0:
            case 1:
            case 2:
            case 3:
        }
    }
cs


이렇게 하게 되면 case 구문을 쓰는 것입니다. case 구문은 if구문의 복잡함을 줄여주는 장점이 있습니다. 코드가 읽기 쉬워집니다. 이 두 가지 방법의 장단점에 대해서는 다음에 다시 포스트하겠습니다.


'Java' 카테고리의 다른 글

[android] 구글맵 3  (0) 2018.02.03
[android] 구글맵 2  (0) 2018.02.03
[android] 구글맵 1  (0) 2018.02.03
[android] Fragment 만들기 2-2  (0) 2018.02.03
[android] Fragment 만들기 2-1  (0) 2018.02.03

+ Recent posts