상세 컨텐츠

본문 제목

[안드로이드]프래그먼트(Fragment) 간의 데이터 전달방법(Listener)

안드로이드

by aries574 2021. 1. 8. 12:30

본문


2021/01/07 - [안드로이드] - [안드로이드] 다이얼로그(Dialog) 사용자정의 (Custom) 화면으로 보여주기

2021/01/06 - [안드로이드] - [안드로이드]다이얼로그(Dialog) 체크박스(CheckBox)로 보여주기

2021/01/05 - [안드로이드] - [안드로이드] 다이얼로그(Dialog) 리스트(List)로 보여주는 방법


이번 시간에는 프래그먼트 간의 데이터 전달하는 간단한 예제를 만들어 보겠습니다. 


1. 프래그먼트 생성

프래그먼트는 2개 생성하며, 이름은 FragmentA, FragmentB로 하겠습니다.

자동으로 [FragmentA.java, fragment_a.xml], [FragmentB.java, fragment_b.xml] 이 생성됩니다.


Fragment_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp"
tools:context=".FragmentA">

<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />

</LinearLayout>


Fragment_b.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".FragmentB"
android:background="@android:color/holo_blue_light"
android:padding="16dp"
android:gravity="center_horizontal"
android:orientation="vertical"
>

<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>

</LinearLayout>


FragmentA.java

package com.kwon.toast;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentA extends Fragment {

private EditText editText;
private Button btnOk;
private FragmentAListener listener;

public interface FragmentAListener{
void onInputASent(CharSequence input);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_a, container, false);

editText = v.findViewById(R.id.edit_text);
btnOk = v.findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence input = editText.getText().toString();
listener.onInputASent(input);
}
});

return v;
}

public void updateEditText(CharSequence newText){
editText.setText(newText);
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if(context instanceof FragmentAListener){
listener = (FragmentAListener)context;
}else{
throw new RuntimeException(context.toString() + " must implement FragmentListner");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}


FragmentB.java

package com.kwon.toast;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentB extends Fragment {

private EditText editText;
private Button btnOk;
private FragmentBListener listener;

public interface FragmentBListener{
void onInputBSent(CharSequence input);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_b, container, false);

editText = v.findViewById(R.id.edit_text);
btnOk = v.findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence input = editText.getText().toString();
listener.onInputBSent(input);
}
});

return v;
}

public void updateEditText(CharSequence newText){
editText.setText(newText);
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if(context instanceof FragmentBListener){
listener = (FragmentBListener)context;
}else{
throw new RuntimeException(context.toString() + " must implement FragmentListner");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}


2. 메인화면 구현(activity_main.xml)

<?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:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<FrameLayout
android:id="@+id/container_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<FrameLayout
android:id="@+id/container_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>


3. 메인코드 구현(MainActivity.java)


import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener{

private FragmentA fragmentA;
private FragmentB fragmentB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fragmentA = new FragmentA();
fragmentB = new FragmentB();

getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}


@Override
public void onInputASent(CharSequence input) {
fragmentB.updateEditText(input);
}

@Override
public void onInputBSent(CharSequence input) {
fragmentA.updateEditText(input);
}
}


4. 실행화면





2021/01/07 - [안드로이드] - [안드로이드] 다이얼로그(Dialog) 사용자정의 (Custom) 화면으로 보여주기

2021/01/06 - [안드로이드] - [안드로이드]다이얼로그(Dialog) 체크박스(CheckBox)로 보여주기

2021/01/05 - [안드로이드] - [안드로이드] 다이얼로그(Dialog) 리스트(List)로 보여주는 방법

반응형

관련글 더보기

댓글 영역