728x90

해당글 원문: https://nanamare.tistory.com/84

 

중첩 프래그먼트와 제어 및 헤드리스 프래그먼트

중첩 프래그먼트의 특징은 XML로 추가할 수가 없고, 항상 동적으로 추가해야한다. 그렇기 때문에 부모 프래그먼트는 자식 프레그먼트를 관리하고 자식프래그먼트가 UI를 담당하는 구조로 자주 ��

nanamare.tistory.com

 

중첩 프래그먼트의 특징은 XML로 추가할 수가 없고, 항상 동적으로 추가해야한다.

그렇기 때문에 부모 프래그먼트는 자식 프레그먼트를 관리하고 자식프래그먼트가 UI를 담당하는 구조로 자주 설계된다.

 

액티비티에서 프래그먼트를 다룰 때는 getSupportFragmentManager()를 사용하였지만 중첩 프래그먼트를 다룰 때는, getChildFragmentManager()를 사용한다.

 

public class TestFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState); //Todo XML Inflate
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        /**
         * 자식 프레그먼트 생성
         */
        FragmentManager childFragment = getChildFragmentManager();
        childFragment.beginTransaction()
                .add(R.id.fragment_container, ChildFragment,getInstance(argments))
                .addToBackStack(null)
                .commit();

        /**
         * 종료
         */
        getChildFragmentManager().popBackStack();

    }
}

 

중첩된 프래그먼트와 뒤로 가기 키를 누를 때, 액티비티에서 부모 프래그먼트를 사용하여, 백스택을 체크하해 제거한다.

 

@Override
public void onBackPressed() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    
    Fragment parent = fragmentManager.findFragmentById(R.id.fragment_container);
    if(parent != null) {
        if(parent.getChildFragmentManager().getBackStackEntryCount() > 0) {
            parent.getChildFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}

 

위에서 중첩 프래그먼트에서는 부모 프래그먼트가 관리를 담당하고 자식이 UI를 담당한다고 했는데, 

그렇다면, 부모는 화면 세로에서 가로모드등 변화로 액티비티가 종료되고 재 생성될 때, Activity.onCreate 콜백이 호출되어 프래그먼트도 다시 생성되게 되는데 굳이 그럴 필요는 없기때문에 이를 막는 코드가 필요하다.

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    
    mFragment = getSupportFragmentManager().findFragmentByTag(TestFragment.TAG);
    if(mFragment != null) {
        mFragment = mFragment.getInstance();
        getSupportFragmentManager().beginTransaction()
                .add(mFragment, TestFragment.TAG)
                .commit();
    }
}
728x90
728x90

AppCompatActivity에서 NoActionBar로 스타일 지정후,

레이아웃에서 별도 Toolbar 지정 후 해당 툴바 타이틀에 text를 설정하려면 아래처럼 하면 된다.

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.layout_example_show);
        setSupportActionBar(binding.toolbar);
        
        //스타일에 별도 설정을 하지 않았다면 앱이름이 나온다. 
        //만약 false 설정하지 않고 setTitle을 하면 앱이름에 가려지니 false처리 할 것
        getSupportActionBar().setDisplayShowTitleEnabled(false); 
        getSupportActionBar().setTitle("My Title");
    }

 

 

Reference

https://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shown

 

 

728x90
728x90

SimpleDateFormat -> 형식, 지역 코드

    public static String getLocaleTimeFromDate(Date date, SimpleDateFormat format){
        String dateStr = "";

        if(date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format.toPattern(), Locale.getDefault());
            dateStr = String.valueOf(sdf.format(date));
        }

        return dateStr;
    }

 

 

Reference

https://sinwho.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%ED%98%84%EC%9E%AC-%EC%A7%80%EC%97%AD-%EC%8B%9C%EA%B0%84-%EA%B5%AC%ED%95%98%EA%B8%B0

728x90
728x90


1. [Manifest -> 해당 Activity]

2. android:windowSoftInputMode="adjustUnspecified|adjustPan" 추

 

        <activity android:name=".menu_club.post.BoardShowActivity"
                android:theme="@style/AppTheme.NoActionBar"
                android:windowSoftInputMode="adjustUnspecified|adjustPan" />

 

Reference

https://musesong.tistory.com/entry/안드로이드-소프트키보드에-가려진-EditText-키보드-위로-올리기

728x90

+ Recent posts