J'ai un ScrollView avec un LinearLayout enfant. J'ajoute TextViews et NumberPickers à LinearLayout par programmation via une méthode View séparée. Cependant, les objets dynamiques ne s'affichent pas lorsque l'onglet contenant le ScrollView est cliqué.Les vues dynamiques ne s'affichent pas dans le fragment: android
Voici mon code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.activity_settings_tab, container, false);
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(getContext());
rootview.findViewById(R.id.scroll_config);
viewFunds();
return rootview;
}
Voici la méthode séparée je l'ai mentionné qui ajoute dynamiquement des objets au LinearLayout:
public View viewFunds(View rootview) {
ScrollView scrollView = new ScrollView(getActivity());
scrollView.findViewById(R.id.scroll_config);
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.findViewById(R.id.ll_config);
//final View linearLayout = getActivity().findViewById(R.id.ll_config);
linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
linearLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//create dynamic objects inside scrollview and dynamic linear layout - horizontal
for (int i = 0; i < res2.getCount(); i++) {
LinearLayout llh = new LinearLayout(getActivity());
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_HORIZONTAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText(/*textArray[i] + " " +*/ res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);
}
return scrollView;
}
Qu'est-ce que je fais mal ici?
vous faites totalement faux vérifier cet exemple comment ajouter des vues runtime https://stackoverflow.com/questions/31047502/how-to-add-view-in-a-linear-layout-dynamically – Pavan
Ceci est dans le contexte de l'utilisation d'un fragment avec des vues dynamiques. L'exemple que vous avez mentionné n'est pas lié à cela. –