2014-04-09 2 views
2

저는 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> 
+0

[this] (http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager?rq=1)이 도움이 될 수 있습니다. –

답변

0

난 당신이 모든 관련 구성 요소를 추가 할 수 있습니다, 문제의 원인을 모르겠습니다 (ViewPager, FragmentPagerAdapter 등)뿐만 아니라?

다음은 나를위한 것입니다. 나는 당신과 같은 생각을 가지고 있었기 때문에, 대체 탐색 기록을 추적 할 수있는 대체 메서드와 함께 제공되는 HostFragment 클래스 내에 대체 기능을 캡슐화했습니다.

public void replaceFragment(Fragment fragment, boolean addToBackstack) { 
    if (addToBackstack) { 
     getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).addToBackStack(null).commit(); 
    } else { 
     getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).commit(); 
    } 
} 

모든 메소드가 수행 방법은 제공된 단편과 R.id.hosted_fragment ID와 프레임 배열을 교체하는 것이다. 이제 필요한 특정 유형의 조각을 호스트하는 HostFragment의 인스턴스로 ViewPager의 페이지를 초기화하면됩니다. 뷰 호출기에서 필요하므로이 작업은 사용자 정의 FragmentPagerAdapter 클래스 내에서 수행됩니다. 클릭 가능한 탭 표시 줄을 표시하기 위해 탭 레이아웃도 포함했습니다.

GitHub에 대한 자세한 내용과 전체 작동 예제를 보려면 tutorial on this topic을 확인하십시오!

0

페이지를 캐시해야하는 것처럼 들립니다. 시도해보십시오.

mViewPager.setOffscreenPageLimit(3); 

뷰어 호출시이 줄을 추가하십시오. 다른 탭으로 이동하면 안드로이드는 왼쪽과 오른쪽 중 하나만 현재 조각을 저장합니다. 따라서 화면 보호 해제 간격을 3으로 설정하면 현재 Android를 저장하고 3은 오른쪽과 3을 저장합니다.