마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

linearlayout로 짠 xml의 fragment간 전환이 안되네요.

0 추천

안녕하세요. 안드로이드 배운지 한달도 안된 초보 개발자입니다.

MainActivity 아래 3개의 Fragment를 두어 탭 메뉴를 둔 프로그램인데요.

3번째 Fragment는 다른 Fragment들과 replace가 잘 되는 반면에 1번째 Fragment에서는 다른 Fragment와의 replace가 안되더군요,

private void callFragment(int frament_no){
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (frament_no){
            case 1:
                HangshowFragment hangshowFragment = new HangshowFragment();
                transaction.replace(R.id.hangshow_layout, hangshowFragment); // hangshow_layout // fragment_container
                transaction.commit();
                break;

            case 2:
                LoginFragment loginFragment = new LoginFragment();
                transaction.replace(R.id.hangshow_layout, loginFragment);
                transaction.commit();
                break;

            case 3:
                SignupFragment signupFragment = new SignupFragment();
                transaction.replace(R.id.hangshow_layout, signupFragment);
                transaction.commit();
                break;
        }
    }

위 코드는 3번째 프래그먼트에서 로그인 및 회원가입 프래그먼트로 이동하는 로직입니다.

 

public class StoreFragment extends Fragment {
    public StoreFragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_store, container, false);

        Button btn1 = view.findViewById(R.id.pet_list_search_btn);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                StoreGridFragment storeGridFragment = new StoreGridFragment();
                transaction.replace(R.id.store_layout, storeGridFragment);
                transaction.commit();
            }
        });

        return view;
    }
}

위 코드는 StoreFragment에서 StoreGridFragment로 넘어가고자 하는 부분인데 onClick 이벤트 리스너 부분의 transaction.replace가 잘 먹히지 않네요.

 

문제의 1번 Fragment와 잘 되는 3번 Fragment의 차이를 보아하니 첫번째 프래그먼트는 xml이 LinearLayout으로 구성되어 있고 세번째 프래그먼트는 RelativeLayout으로 구성되어 있더라구요. 그래서 첫번째 프래그먼트를 RelativeLayout으로 바꾸고 버튼을 클릭하면 화면이 교체가 되긴 하는데 이전 화면과 바꾸고자 하는 화면이 겹쳐서 보입니다.. 되도록이면 RelativeLayout으로 바꾸지 않고 LinearLayout에서 할 수 있는 방법을 찾고 싶은데 몇시간을 생각해봐도 모르겠네요. 처음부터 다시 Fragment의 개념을 잡고 가야하는건가요?

 

<?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="match_parent"
              android:id="@+id/store_layout"
              android:orientation="vertical">

    <LinearLayout
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="7dp"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingBottom="10dp"
        android:background="@color/colorPrimary"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="10dp">

            버튼, 텍스트뷰 위치

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#fff"
            android:orientation="horizontal">

             버튼, 텍스트뷰 위치

        </RelativeLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#f1f1f1"
        android:orientation="vertical">
    </LinearLayout>

    <ListView
        android:id="@+id/pet_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

위 코드는 1번째 Fragment의 xml입니다.

haribo (120 포인트) 님이 2017년 11월 24일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...