저는 Android 개발을 처음 사용하고 있으며, ActionBar 스 와이프가 가능한 탭 모델의 세부 사항에 문제가 있습니다. 나는 각 탭에 조각이 있고 ViewPager가 각각의 새로운 조각을 호출하는 것을 처리한다는 것을 알고 있습니다. 그 후 나는 조금 잃었다.ViewPager 탭 및 파편 교체하기
레이아웃이 4 개인 탭을 만들었지 만 각 탭의 레이아웃을 변경하는 데 문제가 있습니다. 내 연구에서 비어있는 레이아웃으로 조각을 시작한 다음 적절한 시점에 조각을 추가하고 바꿔야합니다.
아래 코드에서 "fragment_search_bar"를 빈 "fragment_search"에 한 번만 추가하려고합니다. 이것은 작동하는 것처럼 보이지만 다른 탭으로 이동하여 돌아올 때 검색 창이 사라집니다. 그 탭에 인접한 검색 탭으로 돌아 가면 다시 나타나는 경우가 있습니다. 이상적으로는 버튼을 눌렀을 때 검색 결과의 다음 부분으로 검색 막대를 바꾸기를 원합니다 (내 현재 문제를 이해하면 쉽게 완료 될 것입니다).
광범위하게 읽은 후에도 프래그먼트 수명주기를 완전히 잘 알고 있지 않을 것이라고 확신합니다.
SearchFragment.java
public class SearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
if (rootView.findViewById(R.id.fragment_search_bar) == null) {
FragmentSearchBar searchBar = new FragmentSearchBar();
searchBar.setRetainInstance(true);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_search, searchBar, "search_bar");
fragmentTransaction.addToBackStack("search_bar");
fragmentTransaction.commit();
}
fragment_search.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="match_parent"
android:orientation="horizontal"
android:id="@+id/fragment_search" >
</LinearLayout>
fragment_search_bar.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="match_parent"
android:orientation="horizontal"
android:id="@+id/fragment_search_bar" >
<EditText android:id="@+id/search_query"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/search_prompt" />
<Button android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search_button" />
</LinearLayout>
[this] (http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager?rq=1)이 도움이 될 수 있습니다. –