从子片段访问父片段布局

时间:2014-02-18 06:16:03

标签: android listview android-fragments tabs

大家好日子。我无法弄清楚如何从子Fragment访问父Fragment布局。假设我在ActionBar中定义了标签的主要活动。每个标签都是片段。现在在其中一个标签中我想再次使用标签(这次坚持到底部)。我可以使用经典的TabHost创建所需的布局。这些子选项卡中的每一个都将由相同的Fragment类操作 - 字面上它应该是普通的ListView,数据库中的数据几乎相同,它只有一个字段(完整列表,“访问”项和“未访问”)。 / p>

所以这是我的父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,我计划根据从父片段传递的参数为数据库设置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;
    }

}

2 个答案:

答案 0 :(得分:0)

现在在我进一步调查期间,我发现以下计划可行。关键参数在布局中移动到调用的“tag”属性。如果有任何缺点请咨询。

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;
    }

}

答案 1 :(得分:0)

在子片段中尝试访问父片段,如下所示:

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

这里“sendbutton”是父片段中的Button。如果需要,您应该在子片段中使用invalidate()来刷新视图。