[Android] Dialog Fragment 통신 개발 테스트

Oxong·2021년 6월 25일

21.06.24

공부한 것을 정리하는 용도의 글이므로 100% 정확하지 않을 수 있습니다.
참고용으로만 봐주시고, 내용이 부족하다고 느끼신다면 다른 글도 보시는 것이 좋습니다.
+ 틀린 부분, 수정해야 할 부분은 언제든지 피드백 주세요. 😊

                                                 by. ryalya



구현방향

  • 부모 Activity가 될 MainActivity를 CustomDialog형식으로 생성한다.
  • Fragment1(WebView)와 Fragment2(QR화면)의 Fragment를 생성한다.
  • Framgent1과 Fragment2는 버튼을 클릭하여 동적으로 교체된다.
  • Fragment1과 Fragment2는 보여지는 화면이 다를 뿐, 값을 통신하며 같은 데이터를 가지고 있다.
  • Fragment간 통신은 Bundle을 사용한다.


XML의 Layout은 Relative Layout을 사용했다.
Linear Layout처럼 위치에 일일히 dp를 주는 것 보다는 View 하나를 기준으로 위치하게 하는 것이 다른 디바이스에서도 Content들이 잘 위치해 있을 것 같아서이다.


MainActivity

MainActivity에서 학생증 버튼을 클릭했을 때, DialogFragment의 객체를 불러줘서 onCreateView()를 띄워주도록 했다.

    public void showStudentCard(){
        try{
            MyDialogFragment dialog = MyDialogFragment.getInstance();
            dialog.show(getSupportFragmentManager(), "MyDialogFragment");
        }catch (Exception e){
            Log.e("테스트", "error >>>> " + e);
        }
    }

아니 그런데ㅠㅠㅠㅠㅠ DialogFragment의 사이즈를 조절하는 방법이 하나도 안먹힌다...

진심 구글링하면서 나온 아래 방법 다 사용해봤는데 하나도 안먹힘....

DialogFragment Width,Height(Size)조정

  1. 리소스 dimen.xml 사용
  2. WindowManager 사용
  3. get Display() 사용
  4. ViewGroup 사용
  5. root element layout parameter
  6. min/max width 지정

1. resources values 'dimen'

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);
// xml (Layout)
android:layout_width="match_parent"
android:layout_height="match_parent"
// onResume()
        int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
        int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
        getDialog().getWindow().setLayout(width, height);

2. WindowManager

        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height =  ViewGroup.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setLayout((int) (params.width*0.7), (int) (params.height*0.6));
        params.gravity = Gravity.CENTER;
        getDialog().getWindow().setAttributes(params);

3. Display

 Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics ();
        display.getMetrics(outMetrics);
        dialog.getWindow().setLayout((int)(312 * outMetrics.density), (int)(436 * outMetrics.density));

4. ViewGroup

        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = LayoutParams.MATCH_PARENT;
        params.height = LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

5. root element layout parameter

int width = activity.getResources().getDisplayMetrics().widthPixels;
int height = activity.getResources().getDisplayMetrics().heightPixels;
content.setLayoutParams(new LinearLayout.LayoutParams(width, height));

6. min/max width 지정

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/background_material_light">
      .....

DialogFragment의 사이즈 조절이 안되다보니 Fragment1과 Fragment2를 동적으로 교체할 버튼 이미지를 WebView 오른쪽에 배치하면 아예 안뜸....
(아래에 배치하면 뜸)


구현방향 변경

DialogFragment대신 중간 영역을 Context(Fragment)영역으로 잡아서 Fragment로 구현해보기로 함.


MainActivity

main.xml의 구역을 3등분으로 나눴다.

아래 그림처럼 면적의 80%를 Frame Layout으로 10%는 버튼1, 10%는 버튼2로 구현했다.

버튼 1의 경우, 버튼 2를 눌렀을 때 VISIBLE되는 형태로 Default는 GONE 상태이다.

Frame Layout은 아래 세가지 XML이 Frame Layout안에서 특정 클릭 이벤트를 통해 교체된다.

  • default.xml (처음 실행시 띄워져 있는 기본 화면)
  • studentCard.xml (FragmentA)
  • studentQR.xml (FragmentB)

버튼2를 studentCard.xml 화면이 나타나고, 버튼1의 상태가 VISIBLE 상태가 된다.

버튼 1을 클릭하면 클릭 상태를 true&false로 구분하여 studentCard.xml와 studentQR.xml이 번갈아가며 나타난다.


FragmentA는 Custom Dialog때와 동일하다.
1. 서버에서 Push 메세지를 통해 xhtml코드가 String으로, Json으로 데이터 값이 옴.
2. 데이터를 MainActivity로 보냄.
(but 현재는 테스트를 위해 임의의 값을 지정해 놓음)
3. 버튼을 누르면 여러 데이터를 담아 FragmentA로 보내주는 코드를 작성. (구현 예정)
4. 웹뷰로 HTML을 파싱하여 띄워줌


FragmentB
1. 버튼 클릭시 FragmentA에 있는 데이터를 담아 FragmentB로 보내주는 코드 작성.
2. 정보를 QR이미지로 띄워줌.
3. QR 이미지 클릭시 확대.




Reference

Relative Layout :
https://recipes4dev.tistory.com/126
DialogFragment Resize :
https://developerarea.tumblr.com/post/139280308210/how-to-set-dialogfragments-width-and-height

0개의 댓글