안드로이드 프레그먼트(Fragment) 예제2 , 버튼 클릭할 때마다 이미지 바꾸기 , 두개의 Fragment간의 통신


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
public class MainActivity extends AppCompatActivity implements TabFragment.ImageSelectionCallback {
 
    TabFragment tabFragment;
    ViewerFragment viewerFragment;
 
    int []images = {R.drawable.dream01, R.drawable.dream02, R.drawable.dream03};
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
 
        FragmentManager manager = getSupportFragmentManager();
 
        tabFragment = (TabFragment)manager.findFragmentById(R.id.listFragment);
        viewerFragment = (ViewerFragment)manager.findFragmentById(R.id.viewerFragment);
    }
 
 
    @Override
    public void onImageSelected(int position) {
        viewerFragment.setImage(images[position]);
    }
}
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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <fragment
        android:id="@+id/listFragment"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:name="com.ghj.ex_01_2.TabFragment" />
 
    <fragment
        android:id="@+id/viewerFragment"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.ghj.ex_01_2.ViewerFragment" />
 
</LinearLayout>
 
cs


TabFragment.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
public class TabFragment  extends Fragment {
 
    public ImageSelectionCallback callback;
 
    //인터페이스 - 탭 클릭시 이미지 변경하기 위한 인터페이스
    //여러 액티비티가 fragment를 호출하여도 동일한 인터페이스를 구현하도록 한다
    public static interface ImageSelectionCallback{
        public void onImageSelected(int position);
    }
 
 
    //fragment가 activity에 연결되었을때 호출
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
 
        if(context instanceof ImageSelectionCallback){
            callback = (ImageSelectionCallback)context;
        }
    }
 
    //fragment 뷰 계층을 리턴
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_tab, container, false);
 
        //UI
        Button btn1 = (Button)rootView.findViewById(R.id.btnImg1);
        Button btn2 = (Button)rootView.findViewById(R.id.btnImg2);
        Button btn3 = (Button)rootView.findViewById(R.id.btnImg3);
 
        //이벤트
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(0);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(1);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                callback.onImageSelected(2);
            }
        });
 
        return rootView;
    }
}
 
cs



fragment_tab.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/btnImg1"
        android:text="첫번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <Button
        android:id="@+id/btnImg2"
        android:text="두번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 
    <Button
        android:id="@+id/btnImg3"
        android:text="세번째"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>
cs


ViewerFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ViewerFragment extends Fragment {
 
    ImageView imageView;
 
    @Nullable
    @Override
    public View onCreateView(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 resId){
        imageView.setImageResource(resId);
    }
}
 
cs



fragment_viewer.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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">
 
    <!-- android:scaleType="centerInside" : 이미지뷰에 맞춰 이미지의 크기를 동일한 비율로 맞춘다 -->
    <ImageView
        android:id="@+id/imageView"
        android:scaleType="centerInside"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</LinearLayout>
cs



결과

  


+ Recent posts