ScrollView末尾的TableLayout怪异行为

时间:2018-08-24 09:34:18

标签: android scrollview tablelayout

我正在以编程方式在TableLayouts中创建一些ScrollView,并且可以通过动态添加/删除一些TableLayout来更改这些TableRows的内容。

问题是,除了最后一个没有明显原因的奇怪行为之外,其他所有东西都正常运行。

当我在“问题”上方的TableRows中添加/删除一些TableLayouts时,其中的一些单元格突然在列中显示内容,每列一个字符,然后熄灭单元格而没有拉伸单元格,那是使内容不可读的原因。

我不知道为什么会这样,因为这是唯一一个发生此问题的人。 我认为该问题可能与以下事实有关:ScrollView在调整其内容时会自动调整大小,而“有问题的” TableLayoutScrollView的末尾这会引起一些意想不到的变化。

我应该如何解决这个问题?

代码:

private List<List<View>> addModifiableLinesToView(final TableLayout view, final String fichier,
                                                  final int[] ordreVoulu, final TreeMap<String,ArrayList> map, final RadioGroup rg) {
    final List<List<View>> listRes = new ArrayList<>();

    try{
        //Recherche de valeurs dans le fichier CSV du module en question ex: csv_bt.csv
        InputStream is = new FileInputStream(Environment.getExternalStorageDirectory() +
                "/" + folder_main + "/" + imported_data + "/" + fichier);
        CSVFile csv = new CSVFile(is);
        List<String[]> tableau = csv.findLines(indicePoste.getText().toString());
        List<View> tempList;

        //Pour chaque ligne avec le bon numéro de poste trouvé dans le csv
        for (String[] s : tableau) {
            tempList = new ArrayList<>();
            //met les valeurs dans le bon ordre pour les ajouter au tableau
            String[] vals = filtreValeurs(s, ordreVoulu);
            final TableRow row = new TableRow(getApplicationContext());
            row.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));

            int n = 0;
            //ajoute les valeurs récupérées a une ligne du tableau
            for (String valeur : vals) {

                View divider = new View(getApplicationContext());
                divider.setLayoutParams(new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
                divider.setBackgroundColor(Color.BLACK);

                final ViewSwitcher viewSwitcher = new ViewSwitcher(getApplicationContext());

                TextView t = new TextView(MainActivity.this);
                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                t.setLayoutParams(params1);
                t.setMaxWidth(0);
                if(fichier.equals("csv_amm.csv") && n == 3)
                {
                    valeur = valeur.trim();
                    int val = !valeur.equals("")?Integer.parseInt(valeur) : 0;
                    AMMValue ammval = new AMMValue(val);
                    t.setText(ammval.getLibelle());
                }
                else
                    t.setText(valeur);

                t.setGravity(Gravity.CENTER);
                t.setTextColor(Color.BLACK);
                t.setTextSize(14);
                t.setPadding(5,5,5,5);
                t.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(rg.getCheckedRadioButtonId() != -1)
                            viewSwitcher.showNext();
                    }
                });

                viewSwitcher.addView(t,
                        new TableRow.LayoutParams(
                                TableRow.LayoutParams.MATCH_PARENT,
                                TableRow.LayoutParams.MATCH_PARENT, 1f));

                final String key = (String)map.keySet().toArray()[n];

                if(key.contains("EditText"))
                {
                    final EditText e = new EditText(MainActivity.this);
                    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    e.setLayoutParams(params2);
                    e.setSingleLine();
                    e.setImeOptions(EditorInfo.IME_ACTION_DONE);
                    e.setBackgroundColor(getResources().getColor(R.color.orange));
                    e.setMaxWidth(0);
                    e.setTextSize(14);
                    e.setPadding(5,5,5,5);
                    e.setGravity(Gravity.CENTER);
                    //En fonction de la valeur va afficher un clavier numérique ou non
                    if(key.contains("Number"))
                        e.setInputType(InputType.TYPE_CLASS_NUMBER);
                    else if(key.contains("Text"))
                        e.setInputType(InputType.TYPE_CLASS_TEXT);
                    /*
                     *TextChangedListener qui permet de retourner au mode "TextView"
                     *quand on supprime tout ce qui à été écris
                     */
                    e.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }
                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            if(e.length() == 0)
                            {
                                viewSwitcher.showPrevious();
                                cacherClavierVirtuel();
                            }
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });

                    viewSwitcher.addView(e,
                            new TableRow.LayoutParams(
                                    TableRow.LayoutParams.MATCH_PARENT,
                                    TableRow.LayoutParams.MATCH_PARENT, 1f));
                }
                else if (key.contains("Spinner"))
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            final Spinner spin = new Spinner(getApplicationContext());
                            spin.getBackground().setColorFilter(
                                    getResources().getColor(R.color.noir),
                                    PorterDuff.Mode.SRC_ATOP);
                            spin.setBackgroundColor(getResources().getColor(R.color.orange));
                            spin.setGravity(Gravity.CENTER);
                            spin.setPopupBackgroundResource(R.color.blanc);
                            spin.setPadding(5,5,5,5);
                            ArrayList<String> list = map.get(key);
                            final ArrayAdapter<String> adapter = new ArrayAdapter<>(
                                    MainActivity.this,
                                    R.layout.spinner_adapter,
                                    list);
                            adapter.setDropDownViewResource(
                                    android.R.layout.simple_spinner_dropdown_item);
                            spin.setAdapter(adapter);

                            //Set de la selection à 1 pour ne pas annuler tout de suite
                            spin.setSelection(1);
                            /* ItemListener qui va retourner le ViewSwitcher en mode
                             * affichage si l'on selectionne la valeur index 0 qui
                             * correspond à "Annuler" */
                            spin.setOnItemSelectedListener(
                                    new AdapterView.OnItemSelectedListener() {
                                        @Override
                                        public void onItemSelected(
                                                AdapterView<?> adapterView, View view, int i, long l) {
                                            ((TextView) adapterView.getChildAt(0)).setTextSize(14);
                                            if (adapterView.getSelectedItemPosition() == 0 && rg.getCheckedRadioButtonId() != -1) {
                                                spin.setSelection(1);
                                                viewSwitcher.showPrevious();
                                            }

                                        }

                                        @Override
                                        public void onNothingSelected(AdapterView<?> adapterView) {

                                        }
                                    });

                            viewSwitcher.addView(spin,
                                    new TableRow.LayoutParams(
                                            TableRow.LayoutParams.MATCH_PARENT,
                                            TableRow.LayoutParams.MATCH_PARENT, 1f));
                        }
                    });
                }


                tempList.add(viewSwitcher);

                row.addView(divider,
                        new TableRow.LayoutParams(
                                TableRow.LayoutParams.MATCH_PARENT,
                                TableRow.LayoutParams.MATCH_PARENT));
                row.addView(viewSwitcher,
                        new TableRow.LayoutParams(
                                TableRow.LayoutParams.MATCH_PARENT,
                                TableRow.LayoutParams.MATCH_PARENT, 1f));
                n++;
            }

            //separateur en fin de ligne
            View divider = new View(getApplicationContext());
            divider.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.MATCH_PARENT));
            divider.setBackgroundColor(Color.BLACK);
            row.addView(divider,
                    new TableRow.LayoutParams(
                            TableRow.LayoutParams.MATCH_PARENT,
                            TableRow.LayoutParams.MATCH_PARENT));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    view.addView(row);
                }
            });

            //separateur horizontal
            final View dividerH = new View(getApplicationContext());
            dividerH.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    3));
            dividerH.setBackgroundColor(Color.BLACK);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    view.addView(dividerH, new TableLayout.LayoutParams(
                            TableLayout.LayoutParams.MATCH_PARENT,
                            3));
                }
            });
            listRes.add(tempList);
        }
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
        e.printStackTrace();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,
                        "Erreur de format du CSV pour : " + view.getTag().toString(),
                        Toast.LENGTH_LONG).show();
            }
        });

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return listRes;

0 个答案:

没有答案