Android: Can you nest Fragments?

自作多情 提交于 2019-11-26 04:50:07

问题


Is it possible to stick a Fragment in the layout of another Fragment? Has anyone tried this?


回答1:


Finally! the android 4.2 update comes with default support for nested fragments, also compatible: Official NestedFragments. What's more, the support of the nested Fragments in announced in the latest 11th revision of the v4 support library!




回答2:


No, fragments are currently NOT in a hierarchy. Trying to have one fragment embedded in another can lead to problems, often subtle.




回答3:


Edit: As of ACLv11 this is only needed if you replace(...) ViewPagers dynamically.

Yes you can, but there are certain ways that are a pain, but I have even embedded FragmentViewPagers.

If you embed in XML then when the layout is inflated you will have no problems.

If you embed fragments dynamically within another fragment then you will get issues! Although it is possible.

Generally involves posting to a handler which then executes after the current fragment transition then starts another, this way you can embed fragments pretty easily. But this leads to other issues, so you have to be careful how you implement this.

FoodTestFeast has embedded ViewPagers which are removed and added dynamically.

My Base FragmentViewPager - Fragment that holds a view pager (Which has FragmentPagerAdapter)

I haven't seen any crashes using this yet, but let me know if anyone else does.

/**
 * @project FoodFeast
 * @author chris.jenkins
 * @created Dec 28, 2011
 */
package couk.jenxsolution.food.ui.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.viewpagerindicator.TabPageIndicator;

import couk.jenxsolution.food.R;
import couk.jenxsolution.food.app.FoodApplication;

/**
 * @author chris.jenkins
 */
public class QuizPagerBaseFragment extends QuizBaseFragment
{

    // Data
    private FragmentStatePagerAdapter mAdapter = null;

    // Views
    private int layoutRes;
    private View mRootView;
    private ViewPager mPager;
    private TabPageIndicator mIndicator;

    // Handlers
    private final Handler handler = new Handler();
    private Runnable runPager;
    private boolean mCreated = false;

    /**
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     *      android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        mRootView = inflater.inflate(layoutRes, container, false);
        mPager = (ViewPager) mRootView.findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(2);
        mIndicator = (TabPageIndicator) mRootView.findViewById(R.id.indicator);
        mIndicator.setTypeface(FoodApplication.getInstance().getTFs().getTypeFaceBold());

        return mRootView;
    }

    /**
     * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        if (runPager != null) handler.post(runPager);
        mCreated = true;
    }

    /**
     * @see android.support.v4.app.Fragment#onPause()
     */
    @Override
    public void onPause()
    {
        super.onPause();
        handler.removeCallbacks(runPager);
    }

    /**
     * Set the ViewPager adapter from this or from a subclass.
     * 
     * @author chris.jenkins
     * @param adapter
     */
    protected void setAdapter(FragmentStatePagerAdapter adapter)
    {
        mAdapter = adapter;
        // mAdapter.notifyDataSetChanged();
        runPager = new Runnable() {

            @Override
            public void run()
            {
                mPager.setAdapter(mAdapter);
                mIndicator.setViewPager(mPager);
                // mAdapter.notifyDataSetChanged();
                mIndicator.setVisibility(View.VISIBLE);
            }
        };
        if (mCreated)
        {
            handler.post(runPager);
        }
    }

    /**
     * Has to be set before onCreateView
     * 
     * @author chris.jenkins
     * @param layoutRes
     */
    protected void setLayoutRes(int layoutRes)
    {
        this.layoutRes = layoutRes;
    }

}

FYI your layout will need to look like this (or similar): NOTE THE Indicator = GONE!

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

    <com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:visibility="gone"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>



回答4:


A smart way to solve the impossibility to use nested fragments is to use both v4 and native fragments. It works (I use it in many apps), and in some cases is what we need to bypass this limit.

Obviously, in the activity you can use getFragmentManager() or getSupportFragmentManager() to distinguish the 2 groups of fragments.

Probably is a deprecated use of it, but it solves me many situations.



来源:https://stackoverflow.com/questions/6221763/android-can-you-nest-fragments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!