본문으로 바로가기

Fragment 기본 만들 어 보기

category ANDROID 2018. 8. 2. 13:33

Fragment 슬라이드와  화면 전환 베이식 모드입니다 



Java파일 

1.MainActivty


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
public class MainActivity extends AppCompatActivity  {
    ViewPager vp;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        vp = (ViewPager)findViewById(R.id.vp);
        Button btn_first = (Button)findViewById(R.id.btn_first);
        Button btn_second = (Button)findViewById(R.id.btn_second);
 
        vp.setAdapter(new pagerAdapter(getSupportFragmentManager()));
        vp.setCurrentItem(0);
 
        btn_first.setOnClickListener(movePageListener);
        btn_first.setTag(0);
        btn_second.setOnClickListener(movePageListener);
        btn_second.setTag(1);
 
    }
 
 
    View.OnClickListener movePageListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int tag = (int) v.getTag();
            vp.setCurrentItem(tag);
        }
    };
 
 
 
    private class pagerAdapter extends FragmentStatePagerAdapter implements OnMapReadyCallback {
 
        private GoogleMap mMap;
 
        public String loadJSONFromAsset() {
            String json = null;
            try {
 
                InputStream is = getAssets().open("h1.json");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                json = new String(buffer, "UTF-8");
 
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
            return json;
        }
 
        public pagerAdapter(android.support.v4.app.FragmentManager fm)
        {
            super(fm);
        }
        @Override
        public android.support.v4.app.Fragment getItem(int position)
        {
            switch(position)
            {
                case 0:
                    return new FirstFragment();
 
                case 1:
                    return new SecondFragment();
                default:
                    return null;
            }
        }
 
 
 
        @Override
        public int getCount()
        {
            return 2;
        }
 
    }
 
 
cs


2.FirstFragment

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
public class FirstFragment extends Fragment {
    MapView mapView;
   
 
 
    public FirstFragment() {
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
 
        SupportMapFragment mapView = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
        mapView.onCreate(savedInstanceState);
        mapView.onResume();
        mapView.getMapAsync(this);
        return view;
    }
 
 
    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }
 
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }
 
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onLowMemory();
    }
 
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
 
//액티비티가 처음 생성될 때 실행되는 함수
 
        if (mapView != null) {
            mapView.onCreate(savedInstanceState);
        }
    }
 
 
}
 
cs


3.SecondFragment


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
public class SecondFragment extends Fragment {
    private MapView mapView = null;
    public SecondFragment()
    {
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_second, nullfalse);
 
        SupportMapFragment mapView = (SupportMapFragment)this.getChildFragmentManager().findFragmentById(R.id.map);
        mapView.getMapAsync(this);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }
 
    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }
 
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onLowMemory();
    }
 
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
 
//액티비티가 처음 생성될 때 실행되는 함수
 
        if (mapView != null) {
            mapView.onCreate(savedInstanceState);
        }
    }
 
 
 
}
 
cs



xml파일 


1.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
43
44
45
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    >
<LinearLayout
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/toolbarColor"
    >
    <Button
        android:id="@+id/btn_first"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="응급실"
        android:background="@color/toolbarColor"
        android:textColor="#ffffff"
        android:textSize="20sp"
        />
 
    <Button
        android:id="@+id/btn_second"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="약국"
        android:background="@color/toolbarColor"
        android:textColor="#ffffff"
        android:textSize="20sp"
        />
 
</LinearLayout>
 
<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/ll">
</android.support.v4.view.ViewPager>
 
</RelativeLayout>
 
cs


2.fragment_first.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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30dp"
        android:textStyle="bold"
        android:text="응급실 지도 맵"/>
   <!-- <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />-->
 
</RelativeLayout>
 
cs


3.fragment_second.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30dp"
        android:textStyle="bold"
        android:text="약국 지도 맵"/>
 
</RelativeLayout>
 
cs



실행에 이상이 있으신 분은 꼭 댓글 남겨 주세요