728x90
반응형
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
//여기서는 메인액티비티를 나타냄
if(context instanceof imageSelectionCallback ){
callback = (imageSelectionCallback) context;
}
}
@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 = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback!=null){
callback.onImageSelected(1);
}
}
});
package com.threedpit.myfragment2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.os.Bundle;
import android.widget.ViewSwitcher;
import java.util.List;
public class MainActivity extends AppCompatActivity implements imageSelectionCallback{
ListFragment listFragment;
Viewerfragment viewerfragment;
//이미지자체를 배열에 넣어 인덱스 0,1,2를 가지게함
int[] images = {R.drawable.spring,R.drawable.summer,R.drawable.fall};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//메인액티비티에 프래그먼트를 띄우기 위한 초기 설정
FragmentManager manager = getSupportFragmentManager();
listFragment = (ListFragment) manager.findFragmentById(R.id.listFragment);
viewerfragment = (Viewerfragment) manager.findFragmentById(R.id.viewerFragment);
}
//이미지를 바꾸게 하기 위한 메소드 implements 구현을 사용하여 메소드 오버라이딩하여 사용
// public 으로 쓰여있어서 어디서는 실행이 가능
@Override
public void onImageSelected(int position) {
//이렇게하면 뷰어프래그먼트 화면에 이미지를 띄우겠다 생각하면된다.
viewerfragment.setImage(images[position]);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<fragment
android:id="@+id/listFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.threedpit.myfragment2.ListFragment"/>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<fragment
android:id="@+id/viewerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.threedpit.myfragment2.Viewerfragment"
/>
</FrameLayout>
</LinearLayout>
package com.threedpit.myfragment2;
import android.Manifest;
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 {
//구현 클래스 부분 변수 하나 생성해서
imageSelectionCallback callback;
@Override
public void onAttach(@NonNull Context context) {
//프래그먼트가 액티비티에 나타날때 제일 먼저 실행되는 메소드
super.onAttach(context);
//여기서는 메인액티비티를 나타냄
//onAttach에서 받아지는 부분이 현재 프래그먼트가 띄워지는 액티비티
//여기서는 메인 액티비티
if(context instanceof imageSelectionCallback ){
callback = (imageSelectionCallback) context;
}
}
@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 = rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//프래그먼트는 액티비티 위에 생성되는것으로
//혹시나 액티비티가 없는 경우가 있을수 잇으니
// 널 값을 확인 하고 이미지 변경해주는 메소드 실행
if(callback!=null) {
callback.onImageSelected(0);
}
}
});
//여름 화면 나타내는 버튼
Button button2 = rootView.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback!=null){
callback.onImageSelected(1);
}
}
});
//가을 화면 나타내는 버튼
Button button3 = rootView.findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callback!=null){
callback.onImageSelected(2);
}
}
});
return rootView;
}
}
<?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="봄" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="여름" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="가을" />
</LinearLayout>
package com.threedpit.myfragment2;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 = rootView.findViewById(R.id.imageView);
return rootView;
}
public void setImage(int resID){
//이미지 변경해주는 메소드
imageView.setImageResource(resID);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="wrap_content"
app:srcCompat="@drawable/spring" />
</LinearLayout>
728x90
반응형
'안드로이드(Android)' 카테고리의 다른 글
안드로이드 이론 빡공 13 (0) | 2020.06.19 |
---|---|
안드로이드 이론 빡공 12 (0) | 2020.06.19 |
안드로이드 이론 빡공 10 (0) | 2020.06.16 |
안드로이드 이론 빡공 9 (0) | 2020.06.15 |
안드로이드 이론 빡공 8 (0) | 2020.06.14 |
댓글