【问题标题】:Accessing parent Fragment layout from child Fragment从子 Fragment 访问父 Fragment 布局
【发布时间】: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


    【解决方案1】:

    现在,在我进一步调查期间,我发现以下方案可行。关键参数移动到布局中调用的“标签”属性。如有不足请指教。

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

    【讨论】:

      【解决方案2】:

      在子片段中尝试像这样访问父片段:

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

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

      【讨论】:

      • 如果我们想在子片段中获取字符串而不使用公共静态数据类型的引用怎么办?
      • 如果数据类型在子片段的父片段中不公开,则无法访问该数据类型
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多