1

ProblèmeFragment intérieur ViewPager intérieur ViewGroup

Comme le montre la figure ci-dessous, j'ai deux onglets appelés « température » qui sont créés de la même manière (voir mainFragment). Dans ces deux onglets, j'ai un viewpager pour avoir des graphismes précédents (05 août, 04 août, ...). Le premier onglet fonctionne bien mais dans la seconde, rien n'apparaît. Lors du débogage, je peux voir que le fragment est créé dans les deux cas. C'est seulement sur le deuxième onglet où le problème est. Plus encore, je peux voir le viewpager en train de glisser vers la gauche ou la droite. Je cherche depuis des heures maintenant et je ne comprends pas où est le problème. Pourquoi les fragments n'apparaissent pas? J'utilise android.support.v13.app.FragmentStatePagerAdapter pour l'adaptateur.

Voici un court-métrage pour montrer le problème: https://www.dropbox.com/s/vf8qlrd75mdwijp/device-2017-08-06-214510.mp4?dl=0

enter image description here

code

MainFragment

public class MainFragment extends Fragment implements BTListener { 


// the different ID for the views. 
private final static int ID_TEMPERATURE = 1; 

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

    final View rootView = inflater.inflate(R.layout.main,container,false); 

    final ViewGroup mainContainer = (ViewGroup) rootView.findViewById(R.id.main_container); 

    createTemperatureTab(mainContainer); 
    createTemperatureTab(mainContainer); 

    return rootView; 
} 

private void createTemperatureTab(ViewGroup mainContainer){ 

    //inflate the view 
    ViewGroup view = (ViewGroup) LayoutInflater.from(getActivity()).inflate(R.layout.main_list,mainContainer,false); 
    ((TextView)view.findViewById(R.id.title_main_list)).setText("Température"); 
    ((TextView)view.findViewById(R.id.information_main_list)).setText("°C"); 
    final ExpandableLayout expandableLayout = (ExpandableLayout) view.findViewById(R.id.expandable_layout); 

    CustomPager viewPager = (CustomPager) view.findViewById(R.id.viewpager); 
    LinearGraphAdapter linearGraphAdapter = new LinearGraphAdapter(getChildFragmentManager()); 
    viewPager.setAdapter(linearGraphAdapter); 
    viewPager.setCurrentItem(linearGraphAdapter.getCount()); 

    expandableLayout.setExpanded(false); 
    expandableLayout.setDuration(500); 
    expandableLayout.setInterpolator(new DecelerateInterpolator()); 

    view.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(expandableLayout.isExpanded()) { 
       expandableLayout.collapse(); 
      } 
      else { 
       expandableLayout.expand(); 
      } 
     } 
    }); 
    mainContainer.addView(view); 
} 

} 

main_list

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dp" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/title_main_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="10dp" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/information_main_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="80dp" 
     android:textSize="20sp" /> 
</LinearLayout> 

<net.cachapa.expandablelayout.ExpandableLayout 
    android:id="@+id/expandable_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <com.example.renaud.aquariumslinding.adapter.CustomPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </com.example.renaud.aquariumslinding.adapter.CustomPager> 

</net.cachapa.expandablelayout.ExpandableLayout> 


</LinearLayout> 

LineGraphFragment

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.linear_graph_layout,container,false); 

    lineChart = (LineChart) rootView.findViewById(R.id.linear_graph); 
    initiateChart(lineChart); // not important for this case 
    drawChart(lineChart,dayOfYear); // not important for this case 

    return rootView; 
} 

linear_graph_layout

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

<TextView 
    android:id="@+id/linear_graph_date" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:elevation="4dp" 
    android:textColor="#fff" /> 
<com.github.mikephil.charting.charts.LineChart 
    android:id="@+id/linear_graph" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

LinearGraphAdapter

public class LinearGraphAdapter extends FragmentStatePagerAdapter { 

public static int MAXIMUM_COUNT_PAGER = 7; 
private int mCurrentPosition = -1; 

public LinearGraphAdapter(FragmentManager fragmentManager){ 
    super(fragmentManager); 
} 
@Override 
public Fragment getItem(int position) { 
    Bundle bundle = new Bundle(); 
    bundle.putInt(Constant.CUSTOM_PAGER_NUMBER,MAXIMUM_COUNT_PAGER-position); 
    Fragment fragment = new LinearGraphFragment(); 
    fragment.setArguments(bundle); 

    return fragment; 
} 

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

@Override 
public void setPrimaryItem(ViewGroup container, int position, Object object) { 
    super.setPrimaryItem(container, position, object); 
    if (position != mCurrentPosition) { 
     Fragment fragment = (Fragment) object; 
     CustomPager pager = (CustomPager) container; 
     if (fragment != null && fragment.getView() != null) { 
      mCurrentPosition = position; 
      pager.measureCurrentView(fragment.getView()); 
     } 
    } 
} 
} 
+0

Avez-vous essayé de vous connecter pour voir si les fragments sont mesurés correctement?Qu'en est-il de vérifier les limites de la mise en page pour voir si vos vues sont réellement visibles, étant donné que votre mise en page extensible commence comme effondré. En ce qui concerne votre vérification null sous setPrimaryItem, je ne sais pas pourquoi c'est là. Si vous vous attendez à ce que ce soit null, vous aurez probablement besoin de le gérer si c'est le cas. Si vous ne vous attendez pas à ce qu'il soit nul, jetez un oeil pour voir s'il y a un cas où c'est le cas. Vos hauteurs de mise en page sont également toutes wrap_content donc si aucune donnée n'est donnée, rien ne sera visible –

+0

Les fragments des deux mises en page extensibles sont de la même taille, bien mesurés. Si je vérifie les limites de la mise en page, nous pouvons voir qu'elles apparaissent bien: http://hpics.li/f7f188c Enfin, dans la fonction setPrimaryItem, je viens de copier un code d'internet. Après avoir regardé dedans, il semble qu'on l'appelle deux fois. La deuxième fois, c'est un élément nul. Je devais vérifier cela plus en profondeur, mais cela ne fait pas partie de la solution. En effet, comme vous le mentionnez, si je change wrap_content à 150dp, et que je commente la fonction "setPrimaryItem", le problème reste le même (voir http://hpics.li/5afaec3). Merci pour votre aide! – renaudyes

Répondre

0

Ok, après une nuit de recherche et de lecture de l'API, j'ai compris. Je ne comprends pas tout mais il semble que la bibliothèque v13 et v4 ait un conflit.

Pour la remplacer, d'une part:

  • Assurez-vous que vous avez appelé getChildFragmentManager() au lieu de getFragmentManager()
  • Créer un ID différent pour tous les différents viewpageryou ont. Ces identifiants doivent comporter au moins 4 chiffres. Bien sûr, il est préférable d'avoir ceux dans R.String
  • Définissez votre ID dans chaque viewpager en appelant setID()
  • Si cela ne fonctionne pas, s'il vous plaît nettoyer et reconstruire votre projet.

Et enfin, la récompense:

enter image description here

J'espère que cela aidera.