列表视图项目最初为空,但当您向外滚动时,它会显示内容

时间:2016-02-19 11:04:13

标签: android listview adapter

我有ListViewBaseAdapter,其列表项是动态创建的。但是,有一个问题。

首先,列表项显得很长且空白,但是当您向下滚动直到它不在屏幕上时,然后将其向后滚动到屏幕上,它可以毫无问题地显示。这是什么问题?

TableLayout tableLayout = new TableLayout(activity);
                        tableLayout.setLayoutParams(params);
                        tableLayout.setGravity(Gravity.CENTER);

                        JSONObject table = new JSONObject(inputs.getJSONObject(i).getString("Data"));
                        int rowCount = table.getJSONArray("Rows").length();
                        int columnCount = table.getJSONArray("Rows").getJSONObject(0).getJSONArray("Column").length();
                        String tableData[][] = new String[rowCount][columnCount];

                        for (int row = 0; row < rowCount; row++) {

                            TableRow tableRow = new TableRow(activity);
                            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                            tableRow.setLayoutParams(layoutParams);
                            if(row%2==1){ tableRow.setBackgroundColor(Color.LTGRAY); }


                            for (int column = 0; column < columnCount; column++) {
                                tableData[row][column] = table.getJSONArray("Rows").getJSONObject(row).getJSONArray("Column").getString(column);

                                TextView tableItem = new TextView(activity);
                                tableItem.setText(table.getJSONArray("Rows").getJSONObject(row).getJSONArray("Column").getString(column));
                                if(columnCount>1){
                                    tableItem.setMaxWidth(holder.innerLinearLayout.getWidth()/columnCount-1);
                                }
                                tableItem.setLayoutParams(layoutParams);
                                tableItem.setGravity(Gravity.CENTER);
                                tableItem.setPadding(8, 0, 8, 0);
                                if (row == 0) tableItem.setTypeface(null, Typeface.BOLD);
                                tableRow.addView(tableItem);
                            }

                            tableLayout.addView(tableRow);

                        }

                        holder.innerLinearLayout.addView(tableLayout);

编辑:

tableItem.setMaxWidth(holder.innerLinearLayout.getWidth()/columnCount-1);

这是导致问题的一行,当我删除它时,高度也是固定的,但是表项宽度被破坏了。

1 个答案:

答案 0 :(得分:0)

我认为问题是holder.innerLinearLayout的宽度为0,直到第一次布局通过。向后滚动视图时会重复使用,因此它们具有宽度。

如果您希望列具有相同的宽度,可能的解决方案是将它们设置为可伸展的,初始宽度为0.

tableLayout.setStretchAllColumns(true);

....

// use TableLayout.LayoutParams on TableLayout's children
TableLayout.LayoutParams rowLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)
tableRow.setLayoutParams(rowLayoutParams);

...

/// TableRow.LayoutParams on TableRow's children
/// with a 0 initial width
TableRow.LayoutParams itemLayoutParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
tableItem.setLayoutParams(itemLayoutParams);