当前位置: 代码迷 >> Android >> Android使用Fragment嵌套Fragment的模式实现界面滑动
  详细解决方案

Android使用Fragment嵌套Fragment的模式实现界面滑动

热度:99   发布时间:2016-04-28 07:00:27.0
Android使用Fragment嵌套Fragment的方式实现界面滑动
// 主Activity
public class HomeActivity extends FragmentActivity implements OnClickListener {
    private TextView footerLeft;
    private TextView footerMiddle;
    private TextView footerRight;

    private Context context;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        initArgs();

        getView2Init();

        initFragment(new Fragment1());

    }

    /**
     * 初始化变量
     */
    private void initArgs() {
        context = HomeActivity.this;
        fragmentManager = getSupportFragmentManager();

    }

    /**
     * 获得布局文件上的控件并初始化
     */
    private void getView2Init() {
        // 获得控件View
        footerLeft = (TextView) findViewById(R.id.tv_footer_left);
        footerMiddle = (TextView) findViewById(R.id.tv_footer_middle);
        footerRight = (TextView) findViewById(R.id.tv_footer_right);
        // 初始化控件View
        footerLeft.setOnClickListener(this);
        footerMiddle.setOnClickListener(this);
        footerRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 当footer中的三个文本控件被点击时,作出回应
        if (v == footerLeft) {
            Toast.makeText(context, "点击了底栏left左边按钮", Toast.LENGTH_SHORT).show();
            updateFragment(new Fragment1(), false);
        } else if (v == footerMiddle) {
            Toast.makeText(context, "点击了底栏middle中间按钮", Toast.LENGTH_SHORT).show();
            updateFragment(new Fragment2(), false);
        } else if (v == footerRight) {
            Toast.makeText(context, "点击了底栏Right右边按钮", Toast.LENGTH_SHORT).show();
            updateFragment(new Fragment3(), false);
        }
    }

    /**
     * 初始化Fragment
     *
     * @param f
     */
    private void initFragment(Fragment f) {
        updateFragment(f, true);
    }

    /**
     * 更新Fragment
     *
     * @param f
     * @param isInit
     */
    private void updateFragment(Fragment f, boolean isInit) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.content, f);
        ft.commit();
    }

}
// 用来实现切换的Adapter
public class MyPagerAdapter1 extends FragmentStatePagerAdapter {
    protected static final String[] SUB_FRAGMENT = new String[] { "Fragment1_1", "Fragment1_2",
            "Fragment1_3" }; // 对应于每个大Fragment的小Fragment

    private int mCount = SUB_FRAGMENT.length;

    public MyPagerAdapter1(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        if (0 == position) {
            return new Fragment1_1();
        } else if (1 == position) {
            return new Fragment1_2();
        } else if (2 == position) {
            return new Fragment1_3();
        } else {
            System.out.println("创建子Fragment1_" + position + "失败");
            return null;
        }
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return SUB_FRAGMENT[position % mCount];
    }

}
// 主Activity下有三个一级Fragment,这是其中之一
public class Fragment1 extends Fragment {
    private MyPagerAdapter1 mAdapter;
    private ViewPager mPager;
    private TitlePageIndicator mIndicator;
    private static int mCurrentSubFragmentSeq = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment1, container, false);
        System.out.println("F1:onCreateView");
        mAdapter = new MyPagerAdapter1(getFragmentManager());
        mPager = (ViewPager) v.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (TitlePageIndicator) v.findViewById(R.id.titles);
        mIndicator.setViewPager(mPager, mCurrentSubFragmentSeq);
        System.out.println("2、mCurrent Sub Fragment Sequence: " + mCurrentSubFragmentSeq);
        mIndicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);

        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                System.out.println("Changed to page " + position);
                mCurrentSubFragmentSeq = position;
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        return v;
    }

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

    }

}
// 每个一级Framgment下面有三个二级Fragment,这是其中之一
public class Fragment1_1 extends Fragment {
    private static final String KEY_CONTENT = "Fragment1:Content";
    private Bundle bundle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            bundle = savedInstanceState.getBundle(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment1_1, container, false);
        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(KEY_CONTENT, bundle);
    }

}
  相关解决方案