如何把viewpager里面的片段在android系统? [英] How to put viewpager inside fragment in android?

查看:217
本文介绍了如何把viewpager里面的片段在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,我可以显示在本次活动上的FrameLayout一些片断。
问题是,我已经viewpager这些片段一个片段,有时这里面viewpager不能显示像它的碎片任何事物是空的。
有人能帮助我吗?

I've an activity and I can show some fragments on framelayout on this activity. The problem is that I've viewpager inside one fragment of these fragments and sometimes this viewpager cannot show any thing like its fragments is empty. Could somebody help me?

推荐答案

要具有 ViewPager 在一个片段,并有碎片在该页面 ViewPager ,你需要使用嵌套的片段。如果你的的minSdkVersion 17或更高,片段的本机实现支持嵌套片段。否则,您将需要使用支持-V13 (或支持-V4 )片段的反向移植。

To have a ViewPager be in a fragment, and to have fragments be the pages in that ViewPager, you need to use nested fragments. If your minSdkVersion is 17 or higher, the native implementation of fragments supports nested fragments. Otherwise, you will need to use the support-v13 (or support-v4) backport of fragments.

然后,你只需要给 getChildFragmentManager()你的 FragmentPagerAdapter FragmentStatePagerAdapter

Then, you just need to give getChildFragmentManager() to your FragmentPagerAdapter or FragmentStatePagerAdapter:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.pagernested;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagerFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.pager, container, false);
    ViewPager pager=(ViewPager)result.findViewById(R.id.pager);

    pager.setAdapter(buildAdapter());

    return(result);
  }

  private PagerAdapter buildAdapter() {
    return(new SampleAdapter(getActivity(), getChildFragmentManager()));
  }
}

(从此示例项目

这篇关于如何把viewpager里面的片段在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆