Viewpager中有动态控件的Viewpager

时间:2014-10-14 17:43:06

标签: android-viewpager viewpagerindicator

我是新手,我会说一点英语,但我会尽力向你解释我的问题。我希望你能帮助我。

好吧,我有一个5页的viewpager。在第3页,我有一些控件和另一个viewpager。在第二个viewpager上,我动态构建控件。当我开始从第1页滚动(viewpager1)到第3页时,viewpager2的控件加载得非常好。但是当我从第3页返回到第1页然后再返回到第3页时,控件不会加载。这段视频就是一个例子:

http://youtu.be/arWJh5zAS7o?hd=1

下面是一些示例代码

使用viewpager父级的活动

public class VisitaActivity extends FragmentActivity {
private static final String[] CONTENT = new String[] { "Planeamiento", "Información", "Pedido", "Avance", "Confirmación" };
private static final int[] ICONS = new int[] {
    R.drawable.perm_group_elipse, R.drawable.perm_group_elipse,
    R.drawable.perm_group_elipse, R.drawable.perm_group_elipse,
    R.drawable.perm_group_elipse };
private TabPageIndicator indicator;
private FragmentPagerAdapter adapter;


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

    adapter = new PrivateAdapter(getSupportFragmentManager());

    ViewPager pager = (ViewPager) findViewById(R.id.viewPagerVisita);
    pager.setAdapter(adapter);

    indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager, 7f);

}

class PrivateAdapter extends FragmentPagerAdapter implements
        IconPagerAdapter {
    public PrivateAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
        case 2:
            fragment = new FragmentAlbum();
            return fragment;
        }
        return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getIconResId(int index) {
        return ICONS[index];
    }

    @Override
    public int getCount() {
        return CONTENT.length;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

}

使用viewpager子级分段父级

public class FragmentAlbum extends Fragment implements OnClickListener, OnPageChangeListener{


private ViewPager viewPager;    
private ufxEditText etePagina;
private ufxTextView tviIr, tviLimite;
private List<AlbumProducto> listadoProductos, listadoProductos2, listadoProductos3, listadoProductos4;
private List<ProductoUnidad> listadoUnidades, listadoUnidades2;
PrivatePagerAdapter adapter;    

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

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_album, container, false);

    viewPager=(ViewPager)rootView.findViewById(R.id.viewPagerProductos);
    adapter = new PrivatePagerAdapter(getFragmentManager());

    cargarDatos();

    adapter.addFragment(FragmentAlbumProducto.newInstance(listadoProductos));
    adapter.addFragment(FragmentAlbumProducto.newInstance(listadoProductos2));
    adapter.addFragment(FragmentAlbumProducto.newInstance(listadoProductos3));
    adapter.addFragment(FragmentAlbumProducto.newInstance(listadoProductos4));

    this.viewPager.setAdapter(adapter);        

    etePagina=(ufxEditText)rootView.findViewById(R.id.etePagina);
    tviIr=(ufxTextView)rootView.findViewById(R.id.tviIr);
    tviLimite=(ufxTextView)rootView.findViewById(R.id.tviLimite);
    tviLimite.setText("/"+String.valueOf((adapter.getCount()))+"  ");
    etePagina.setText("1");
    tviIr.setOnClickListener(this);
    viewPager.setOnPageChangeListener(this);        
    return rootView;        
}

class PrivatePagerAdapter extends FragmentStatePagerAdapter {

    List<Fragment> fragments;

    public PrivatePagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
    }

    public void addFragment(Fragment fragment) {
        this.fragments.add(fragment);
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        return this.fragments.get(arg0);
    }       

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.fragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }  
}

片段儿童

public static FragmentAlbumProducto newInstance(List<AlbumProducto> listadoProductos) {
    FragmentAlbumProducto fragment = new FragmentAlbumProducto();
    fragment.mlistadoProductos=listadoProductos;
    return fragment;
}   

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

    rootView = (ViewGroup) inflater.inflate(R.layout.activity_albumproducto, container, false);
    contentProductos=(ViewGroup)rootView.findViewById(R.id.contentProductos);
    scrollProductos=(ScrollView)rootView.findViewById(R.id.scrollProductos);

    for(AlbumProducto item:mlistadoProductos){          
        productosDinamic(item);         
        for(ProductoUnidad uni:item.getUnidades()){
            unidadesDinamic(uni);
        }        
    } 
    return rootView;
}

1 个答案:

答案 0 :(得分:0)

前几天,我解决了。我在Activity与viewpagerparent(VisitaActivity)和FragmentParent与viewpagerchild(FragmentAlbum)之间创建了一个Fragment中间体。

public class FragmentRoot extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.root_fragment, container, false);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();  
    transaction.replace(R.id.root_frame, new FragmentAlbum());
    transaction.commit();

    return view;
}

}

我认为这不完美,但我需要