2014-02-18 129 views
1

美好的一天。我無法弄清楚如何從子Fragment訪問父Fragment佈局。假設我有在ActionBar中定義的選項卡的主要活動。每個選項卡都是片段。現在在其中一個選項卡中,我想再次使用選項卡(此時堅持到底)。我能夠使用經典的TabHost創建所需的佈局。這些子標籤中的每一個都將由相同的Fragment類操作 - 從字面上看,它應該是普通的ListView,其數據幾乎與數據庫相同,只會有一個字段不同(完整列表,「已訪問」項和「未訪問」)。從兒童片段訪問父代片段佈局

所以這裏是我的父級PlanFragment,放在主Activity的選項卡上。它有TabHost並填充使用PlanListFragment 3個標籤:

public class PlanFragment extends Fragment implements OnTabChangeListener { 

    protected static final String TAG = "PlanFragment"; 

    public static final String TAB_FULL = "full"; 
    public static final String TAB_VISITED = "visited"; 
    public static final String TAB_NOT_VISITED = "not_visited"; 

    private View mRoot; 
    private TabHost mTabHost; 
    private int mCurrentTab; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     mRoot = inflater.inflate(R.layout.fragment_plan, container, false); 
     mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost); 
     setupTabs(); 
     return mRoot; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setRetainInstance(true); 

     mTabHost.setOnTabChangedListener(this); 
     mTabHost.setCurrentTab(mCurrentTab); 
     updateTab(TAB_FULL, R.id.tab_plan_full); 
    } 

    private void setupTabs() { 
     mTabHost.setup(); 
     mTabHost.addTab(newTab(TAB_FULL, R.string.label_tab_plan_full, 
       R.id.tab_plan_full)); 
     mTabHost.addTab(newTab(TAB_VISITED, 
       R.string.label_tab_plan_visited, R.id.tab_plan_visited)); 
     mTabHost.addTab(newTab(TAB_NOT_VISITED, 
       R.string.label_tab_plan_unvisited, R.id.tab_plan_not_visited)); 
    } 

    private TabSpec newTab(String tag, int labelId, int tabContentId) { 
     TabSpec tabSpec = mTabHost.newTabSpec(tag); 
     tabSpec.setIndicator(getActivity().getString(labelId)); 
     tabSpec.setContent(tabContentId); 
     return tabSpec; 
    } 

    @Override 
    public void onTabChanged(String tabId) { 
     if(TAB_FULL.equals(tabId)) { 
      updateTab(tabId, R.id.tab_plan_full); 
      mCurrentTab = 0; 
      return; 
     } 
     if(TAB_VISITED.equals(tabId)) { 
      updateTab(tabId, R.id.tab_plan_visited); 
      mCurrentTab = 1; 
      return; 
     } 
     if(TAB_NOT_VISITED.equals(tabId)) { 
      updateTab(tabId, R.id.tab_plan_not_visited); 
      mCurrentTab = 2; 
      return; 
     } 
    } 


    private void updateTab(String tabId, int placeholder) { 
     FragmentManager fm = getFragmentManager(); 
     if (fm.findFragmentByTag(tabId) == null) { 
      PlanListFragment plan = new PlanListFragment(); 
      Bundle params = new Bundle(); 
      params.putString(PlanListFragment.TAG, tabId); 
      plan.setArguments(params); 
      fm.beginTransaction() 
        .replace(placeholder, plan, tabId) 
        .commit(); 
     } 
    } 

} 

這裏的佈局TabHost:包括每個選項卡上

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="5dp" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="5dp" > 

      <FrameLayout 
       android:id="@+id/tab_plan_full" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <include layout="@layout/plan_list" /> 
      </FrameLayout> 

      <FrameLayout 
       android:id="@+id/tab_plan_visited" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <include layout="@layout/plan_list" /> 
      </FrameLayout> 

      <FrameLayout 
       android:id="@+id/tab_plan_not_visited" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <include layout="@layout/plan_list" /> 
      </FrameLayout> 
     </FrameLayout> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="-4dp" 
      android:layout_weight="0" /> 
    </LinearLayout> 

</TabHost> 

plan_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

最後PlanListFragment中,我計劃根據從父Fragment傳遞的參數爲數據庫設置CursorAdapter。我如何在這裏訪問ListView?

public class PlanListFragment extends ListFragment { 

    protected static final String TAG = "PlanListFragment"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     Bundle params = getArguments(); 
     Log.d(TAG, params.getString(TAG)); 
     return null; 
    } 

} 

回答

0

現在在我的進一步調查中,我發現下面的計劃工作。關鍵參數移至佈局中的「標記」呼叫屬性。如果有任何缺點,請諮詢。

PlanFragment.java

public class PlanFragment extends Fragment implements OnTabChangeListener { 

    protected static final String TAG = "PlanFragment"; 

    public static final String TAB_FULL = "full"; 
    public static final String TAB_VISITED = "visited"; 
    public static final String TAB_NOT_VISITED = "not_visited"; 

    private View mRoot; 
    private TabHost mTabHost; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     mRoot = inflater.inflate(R.layout.fragment_plan, container, false); 
     mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost); 
     setupTabs(); 
     return mRoot; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setRetainInstance(true); 

     mTabHost.setOnTabChangedListener(this); 
     mTabHost.setCurrentTab(0); 
    } 

    private void setupTabs() { 
     mTabHost.setup(); 
     mTabHost.addTab(newTab(TAB_FULL, R.string.label_tab_plan_full, 
       R.id.tab_plan_full)); 
     mTabHost.addTab(newTab(TAB_VISITED, 
       R.string.label_tab_plan_visited, R.id.tab_plan_visited)); 
     mTabHost.addTab(newTab(TAB_NOT_VISITED, 
       R.string.label_tab_plan_unvisited, R.id.tab_plan_not_visited)); 
    } 

    private TabSpec newTab(String tag, int labelId, int tabContentId) { 
     TabSpec tabSpec = mTabHost.newTabSpec(tag); 
     tabSpec.setIndicator(getActivity().getString(labelId)); 
     tabSpec.setContent(tabContentId); 
     return tabSpec; 
    } 

    @Override 
    public void onTabChanged(String tabId) { 
    } 

} 

fragment_plan.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="5dp" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="5dp" > 

      <fragment 
       android:id="@+id/tab_plan_full" 
       android:tag="plan_full" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       class="com.example.collector.PlanListFragment" /> 

      <fragment 
       android:id="@+id/tab_plan_visited" 
       android:tag="plan_visited" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       class="com.example.collector.PlanListFragment" /> 

      <fragment 
       android:id="@+id/tab_plan_not_visited" 
       android:tag="plan_not_visited" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       class="com.example.collector.PlanListFragment" /> 
     </FrameLayout> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="-4dp" 
      android:layout_weight="0" /> 
    </LinearLayout> 

</TabHost> 

plan_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/FrameLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</FrameLayout> 

PlanListFragment.java

public class PlanListFragment extends ListFragment { 

    protected static final String TAG = "PlanListFragment"; 

    public PlanListFragment() {} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.plan_list, container, false); 

     Log.d(TAG,getTag()); 

     return view; 
    } 

} 
0

在子片段嘗試訪問父片段是這樣的:

ParentFragment frag = ((ParentFragment)this.getParentFragment()); 
frag.sendbutton.setVisibility(View.Visible).invalidate(); 

這裏的「sendbutton」在父母片段的按鈕。如果需要,您應該在子片段中使用invalidate()來刷新視圖。

+0

如果我們想在不使用公共靜態數據類型的引用的情況下獲取子片段中的字符串,該怎麼辦? –

+0

如果在子片段的父片段中未公開,則無法訪問數據類型 –