片段Android中的空容器

时间:2015-01-19 09:17:51

标签: android android-fragments fragment viewgroup

我正致力于自动检测片段中的所有孩子。该函数采用每个片段的Viewgroup容器并将所有子进程内部。

但是,当我的片段只有1页时,容器是空的,或者当我有2页时,第一个容器是空的。 (不要尝试更多页面)

Ps:构建时没有错误,片段显示良好

你能帮助我解决这个问题吗,谢谢

这是代码和logcat关联

public class ContentView extends FragmentStatePagerAdapter{
private int idIhm[];
private int nbPage;

public ContentView(FragmentManager fm,int idIhm [],int nbPage ) {
    super(fm);
    this.idIhm = idIhm;
    this.nbPage = nbPage;
}


@Override
public Fragment getItem(int i) {
    if ((i<nbPage)||(i>0)){
        return new IHMControl(idIhm[i]); 
    }
    else
    return null;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return nbPage; //No of Tabs
}

IHMControl类:

public class IHMControl extends Fragment {  
int layout;
static View ihm;
public static HashSet<Element> ElementList = new HashSet<Element>();

public IHMControl (int layout){
    this.layout=layout;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    ihm = inflater.inflate(layout, container, false);
    View currentView=getView();
    ArrayList<View> allViewArrayList =getAllChildren(container);
    Log.d("child","enter");
    for (View child : allViewArrayList){
        Log.d("child","elem");
        if (child instanceof ImageView){
            Log.d("child","LED");
        }
        else if (child instanceof TextView){
            Log.d("child","texte");
        }
        else if (child instanceof Button){
            Log.d("child","button");
        }
    }
    return ihm;
}

public static View getCurrentIHMView(){
return ihm; 
}


private ArrayList<View> getAllChildren(View v){

    if (!(v instanceof ViewGroup)){ 
        ArrayList<View> viewArrayList =new ArrayList<View>();
        viewArrayList.add(v);
        return viewArrayList;
    }
    ViewGroup viewGroup=(ViewGroup)v;
    int nb=viewGroup.getChildCount();

    Log.d("child",""+nb);
    ArrayList<View> result =new ArrayList<View>();
    for (int i=0;i<nb;i++){

        ArrayList<View> viewArrayList =new ArrayList<View>();
        View child= viewGroup.getChildAt(i);
        viewArrayList.add(viewGroup);
        viewArrayList.addAll(getAllChildren(child));

        result.addAll(viewArrayList);
    }

    return result;      
}

logcat

With 1 page

I/ActivityManager(  413): START u0 {cmp=com.projetihm.menu/com.projetihm.afficha
ge.IHMView (has extras)} from pid 16348
D/child   (16348): 0
D/child   (16348): enter


With 2 pages

D/mali_winsys(16348): new_window_surface returns 0x3000
I/ActivityManager(  413): START u0 {cmp=com.projetihm.menu/com.projetihm.afficha
ge.IHMView (has extras)} from pid 16348
D/child   (16348): 0
D/child   (16348): enter
D/child   (16348): 1
D/child   (16348): 1
D/child   (16348): 11
D/child   (16348): 6
D/child   (16348): 9
D/child   (16348): 9
D/child   (16348): 9
D/child   (16348): enter
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): LED
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): LED
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): LED
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): LED
D/child   (16348): elem
D/child   (16348): elem
D/child   (16348): LED
D/child   (16348): elem

编辑:

        for(int i=0;i<Globals.getTabLengh(ihmList.get(position).ihmPageName);++i){                  
            page_selected[i] = getResources().getIdentifier(ihmList.get(position).ihmPageName[i], "xml", getPackageName());
            nbPage = i+1;
        }

我通过他的名字得到了XML文件的id。此xml文件放在文件夹&#34; RES / xml&#34;

 /*******************************************************************************
                        Fragment (slider)  
 *******************************************************************************/
    public void FragmentIHM(){
        // Initialization
        viewPager = (ViewPager) findViewById(R.id.pager);
        //création des vues avec la class ContentView
        mAdapter = new ContentView(getSupportFragmentManager(),page_selected,nbPage);
        viewPager.setAdapter(mAdapter);

        //indicateur personalisé pour savoir sur quelle page on se trouve
        mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        mIndicator.setViewPager(viewPager);

        //action sur le changement de page
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mIndicator.onPageSelected(position);
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // TODO Auto-generated method stub
                mIndicator.onPageScrolled( position, positionOffset, positionOffsetPixels);
            }       
            @Override
            public void onPageScrollStateChanged(int state) {
                // TODO Auto-generated method stub
                mIndicator.onPageScrollStateChanged(state);
            }
        });
    }

Thibault

0 个答案:

没有答案