【发布时间】:2014-02-18 06:16:03
【问题描述】:
大家好。我不知道如何从子片段访问父片段布局。假设我有在 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;
}
}
【问题讨论】:
标签: android listview android-fragments tabs