MainActivity

...더보기
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
package com.example.boostcoursepractice;
 
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
 
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;
 
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    ListFragment fragment1 ;
    ViewerFragment fragment2 ;
 
    FragmentManager manager ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        manager = getSupportFragmentManager();
 
        fragment1 = (ListFragment) manager.findFragmentById(R.id.list_fragment) ;
        fragment2 = (ViewerFragment) manager.findFragmentById(R.id.viewer_fragment);
 
    }
 
    public void onImageChange(int index)
    {
        fragment2.setImage(index);
 
    }
 
}
 
 
cs

 

ListFragment

...더보기
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
package com.example.boostcoursepractice;
 
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
 
public class ListFragment extends Fragment {
 
    MainActivity activity ;
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
 
        activity = (MainActivity) getActivity();
    }
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list, container, false);
 
        Button button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onImageChange(0);
            }
        });
 
        Button button2 = (Button) rootView.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onImageChange(1);
            }
        });
 
        Button button3 = (Button) rootView.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onImageChange(2);
            }
        });
 
        return rootView ;
    }
}
 
cs

 

ViewerFragment

...더보기
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
package com.example.boostcoursepractice;
 
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
 
public class ViewerFragment extends Fragment {
 
    ImageView imageView ;
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_viewer, container, false);
 
        imageView = (ImageView) rootView.findViewById(R.id.imageView);
 
        return rootView ;
    }
 
    public void setImage(int index)
    {
        if(index == 0)
        {
            imageView.setImageResource(R.drawable.ic_12);
        }
 
        else if(index == 1)
        {
            imageView.setImageResource(R.drawable.ic_15);
        }
 
        else if(index == 2)
        {
            imageView.setImageResource(R.drawable.ic_19);
        }
    }
}
 
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
<?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">
 
 
    <fragment
        android:id="@+id/list_fragment"
        android:name="com.example.boostcoursepractice.ListFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></fragment>
 
    <fragment
        android:id="@+id/viewer_fragment"
        android:name="com.example.boostcoursepractice.ViewerFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></fragment>
 
 
</LinearLayout>
cs

 

fragment_list.xml

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이미지1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이미지2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이미지3" />
</LinearLayout>
cs

 

fragment_viewer.xml

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/ic_12" />
</LinearLayout>
 
 

 

프래그먼트는 앞선 글에서와 같이 코드에서 생성하는 방법이 있고 여기에서 처럼 xml내에서 직접 태그로 추가할 수도 있다. 여기서 만약 태그로추가하고 그 프래그먼트를 자바코드에서 직접 참조 하고 싶다면 일반적으로 쓰는 방법과 약간 달리

 

FragmentManager manager ;

manager = getSupportFragmentManager();

fragment1 = (ListFragment) manager.findFragmentById(R.id.list_fragment) ;

 

이렇게 프래그먼트 매저를 통해서 찾아야한다. 

 

여기서 들은 예시는 리스트 프래그먼트와 이미지뷰어 프래그먼트를 둘로 나우어 메인 액티비티에서 띄우고 리스트 프래그먼트의 버튼을 누르면 이미지 프래그먼트의 이미지를 바꿔주는데 이것 역시 중간에 액티비티를 거쳐서 바꿔줘야한다는 점을 잊지말자.

 

 

 

 

 

결과화면

 

+ Recent posts