2015-01-09 4 views
5

이 문제와 관련하여 몇 가지 질문이 있습니다. 예 : this one. 그러나 그것은 효과가 없습니다.FragmentTabHost에서 현재 선택된 조각을 얻는 방법

내 코드에서 수행 한 작업을 보여 드리겠습니다.

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" > 

    <fragment 
     android:id="@+id/my_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.example.zhouhao.test.MyFragment" 
     tools:layout="@layout/fragment_my" /> 

</FrameLayout> 

fragment_my.xml (a FragmentTabHost 포함 내 주요 단편)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="100" 
     android:orientation="horizontal"> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="95" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <FrameLayout 
       android:id="@+id/panel_content" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

fragment_tab1.xml (다른 탭에 대응하는 프래그먼트 , 그들은 하나의 코드 만 보여주기 위해 유사합니다)

(210)
<FrameLayout 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" tools:context="com.example.zhouhao.test.TabFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView android:layout_width="match_parent" android:layout_height="match_parent" 
     android:text="@string/fragment_tab1" /> 

</FrameLayout> 

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (savedInstanceState == null) { 
     FragmentManager fm = getSupportFragmentManager(); 
     mMyFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment); 
    } 
} 

공용 클래스 MyFragment는 조각이 TabHost.OnTabChangeListener를 구현 확장 {

FragmentTabHost mFragmentTabHost; 


public MyFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_my, container, false); 
    if (rootView != null) { 
     mFragmentTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost); 
     mFragmentTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.panel_content); 
     TabFragment1 tab1Fragment = new TabFragment1(); 
     TabFragment2 tab2Fragment = new TabFragment2(); 
     TabFragment3 tab3Fragment = new TabFragment3(); 
     TabHost.TabSpec spec1 = mFragmentTabHost.newTabSpec("1").setIndicator(""); 
     TabHost.TabSpec spec2 = mFragmentTabHost.newTabSpec("2").setIndicator(""); 
     TabHost.TabSpec spec3 = mFragmentTabHost.newTabSpec("3").setIndicator(""); 
     mFragmentTabHost.addTab(spec1,tab1Fragment.getClass(), null); 
     mFragmentTabHost.addTab(spec2,tab2Fragment.getClass(), null); 
     mFragmentTabHost.addTab(spec3,tab3Fragment.getClass(), null); 
     mFragmentTabHost.setOnTabChangedListener(this); 
     return rootView; 
    } else { 
     return super.onCreateView(inflater, container, savedInstanceState); 
    } 
} 

@Override 
public void onTabChanged(String tabId) { 

    BaseFragment f = (BaseFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tabId); 
    Log.d("Log",tabId); 
} 

}

내 문제는 BaseFragment가 항상 널 내 onTabChanged. 아무도 도와 줄 수 있니? 감사.

+0

게시물을 다음을 참조 [findFragmentByTag이 - FragmentTabHost의 조각을 찾고 - 항상 null] (http://stackoverflow.com/questions/16616495/findfragmentbytag-looking-for- fragment-in-fragmenttabhost-always-null) 아마도 도움이 –

+0

이것은 내 게시물에서 언급 한 정확히 동일한 URL입니다. 그러나 그것은 나를 위해 작동하지 않습니다. –

+0

addTab을 사용할 때 HashMap을 사용하여 Fragment를 저장할 수 있습니다. 그러나 탭이 FragmentTabHost에 추가되어 다시 검색 할 수 있습니다. 내가 맞습니까? –

답변

3

조각화 된 적이없는 경우 즉시 선택한 조각을 가져올 수 없습니다.

@Override 
public void onTabChanged(final String tabId) { 
    Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId); 
    Log.d(TAG, "onTabChanged(): " + tabId + ", fragment " + fg); 

    if (fg == null) { 
     new Handler().postDelayed(new Runnable() {    
      @Override 
      public void run() { 
       Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId); 
       Log.d(TAG, "onTabChanged() delay 50ms: " + tabId + ", fragment " + fg);    
      } 
     }, 50); 
    } 
} 

출력 :

// cannot get the selected fragment immediately if the fragment has never been instantiated. 
onTabChanged(): 1, fragment null 
onTabChanged() delay 50ms: 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1} 
onTabChanged(): 2, fragment null 
onTabChanged() delay 50ms: 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2} 

// can get the selected fragment immediately if the fragment already instantiated. 
onTabChanged(): 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1} 
onTabChanged(): 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2} 
관련 문제