활동 그룹 내에서 다른 조각으로 조각 바꾸기
그룹 활동 안에 조각이 있는데 다른 조각으로 바꾸고 싶습니다.
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
SectionDescriptionFragment bdf = new SectionDescriptionFragment();
ft.replace(R.id.book_description_fragment, bdf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
액티비티 그룹을 사용하지 않고 별도의 프로젝트로 수행하면 제대로 작동합니다. 제어가 getview () 내부로 이동하면 로그 고양이에서 모든 것이 잘 작동하지만 아무런보기도 보이지 않으며 예외가 발생하지 않습니다. 섹션 세부 조각으로 대체됩니다.
책 세부 조각의 Xml에는 ID book_description_fragment가 있고 섹션 설명 조각에 대한 xml에는 section_description_fragment라는 ID가 있습니다.
위의 코드는 항목의 onClick 메서드에 있으며 사용자가 가로 스크롤보기에서 항목을 탭하면 조각이 변경되기를 원합니다.
XML로 하드 코딩 된 단편은 대체 할 수 없습니다. 프래그먼트를 다른 프래그먼트로 교체해야하는 경우 우선 동적으로 추가해야합니다.
참고 : R.id.fragment_container 는 조각을 가져올 활동에서 선택한 레이아웃 또는 컨테이너입니다.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
이 질문을 참조하십시오
" 동적으로 추가 된 조각 " 만 바꿀 수 있습니다 .
따라서 동적 조각을 추가하려면 이 예제를 참조하십시오 .
조각 교체 및 수명주기 를 관리하는 완벽한 방법 으로 요지를 만들었습니다 .
현재 프래그먼트가 동일하지 않고 백 스택에 있지 않은 경우 (이 경우 팝이 됨) 현재 프래그먼트를 새 프래그먼트로만 바꿉니다.
조각을 백 스택에 저장하는 것처럼 여러 옵션이 포함되어 있습니다.
=> 여기 요점 참조
이 활동과 단일 활동을 사용하여이를 활동에 추가 할 수 있습니다.
@Override
public void onBackPressed() {
int fragments = getSupportFragmentManager().getBackStackEntryCount();
if (fragments == 1) {
finish();
return;
}
super.onBackPressed();
}
android.support.v4에서 아래 코드를 사용하십시오.
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
WebViewFragment w1 = new WebViewFragment();
w1.init(linkData.getLink());
ft1.addToBackStack(linkData.getName());
ft1.replace(R.id.listFragment, w1);
ft1.commit();
v4를 사용하여이 코드를 사용하십시오.
ExampleFragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
당신이 잘하고 있기를 바랍니다. 작업을 시작했을 때도 Android Fragments같은 문제가 발생했습니다 .1- 다른 것과 조각을 바꾸는 방법에 대해 읽었습니다. 2- 프래그먼트 Fragment container가없는 경우 프래그먼트를 추가하는 방법 .
그런 다음 R & D 후, 지금까지 많은 프로젝트에서 도움이되는 함수를 만들었으며 여전히이 간단한 함수를 사용하고 있습니다.
public void switchFragment(BaseFragment baseFragment) {
try {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
if (getSupportFragmentManager().findFragmentById(R.id.home_frame) == null) {
ft.add(R.id.home_frame, baseFragment);
} else {
ft.replace(R.id.home_frame, baseFragment);
}
ft.addToBackStack(null);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
코드 시간을 즐기십시오 :)
ViewPager를 사용하십시오. 그것은 나를 위해 일하고 있습니다.
final ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.vp_pager);
button = (Button)result.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(1);
}
});
kotlin에서는 다음을 수행 할 수 있습니다.
// instantiate the new fragment
val fragment: Fragment = ExampleFragment()
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.book_description_fragment, fragment)
transaction.addToBackStack("transaction_name")
// Commit the transaction
transaction.commit()
you can use simple code its work for transaction
Fragment newFragment = new MainCategoryFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame_NavButtom, newFragment);
ft.commit();
You Can Use This code
((AppCompatActivity) getActivity()).getSupportFragmentManager().beginTransaction().replace(R.id.YourFrameLayout, new YourFragment()).commit();
or You Can This Use Code
YourFragment fragments=(YourFragment) getSupportFragmentManager().findFragmentById(R.id.FrameLayout);
if (fragments==null) {
getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayout, new Fragment_News()).commit();
}
'Programming' 카테고리의 다른 글
| 데이터베이스에 태그를 저장하는 가장 효율적인 방법은 무엇입니까? (0) | 2020.06.29 |
|---|---|
| data.frame 열 형식을 문자에서 요소로 변환 (0) | 2020.06.29 |
| Git On Custom SSH 포트 (0) | 2020.06.29 |
| URL 해시 위치 가져 오기 및 jQuery에서 사용 (0) | 2020.06.29 |
| 파일 크기를 확인하는 방법? (0) | 2020.06.29 |