Я делаю сетку Судоку 9x9, где каждая из 81 ячеек сама является сеткой 3x3. Одна ячейка выглядит примерно так:
1 2 3
4 5 6
7 8 9
Каждое число представляет карандашные аннотации для этой ячейки. У меня есть файл с именем cell_layout.xml, представляющий это расположение 3x3.
Я уже в состоянии сгенерировать сетку, и код работает:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.solver_principal);
TableLayout sudokuGrid = (TableLayout) findViewById(R.id.sudokuGrid);
sudokuGrid.setShrinkAllColumns(true);
sudokuGrid.setStretchAllColumns(true);
TableRow.LayoutParams paramsRow = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout.LayoutParams paramsLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
for(int i = 0; i < 9; ++i)
{
TableRow tableRow = new TableRow(SolverActivity.this);
tableRow.setDividerDrawable(getResources().getDrawable(R.drawable.column_divider));
tableRow.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
for(int j = 0; j < 9; ++j)
{
View cell = getLayoutInflater().inflate(R.layout.cell_layout, sudokuGrid, false);
cell.setLayoutParams(paramsRow);
tableRow.addView(cell);
}
tableRow.setLayoutParams(paramsLayout);
sudokuGrid.addView(tableRow);
}
}
Приведенный выше код просто увеличивает в 81 разтребуемый макет в TableLayout.
Это работает, так в чем же ваша проблема?
Создание действия занимает слишком много времени. Даже если я тестирую только с одной строкой сетки, метод будет слишком долго раздувать требуемый макет.
Я получаю:
Фоновое параллельное копирование GC освобождается 131244 (9 МБ)) Объекты AllocSpace, 0 (0B) LOS-объектов, 24% свободного, 74 МБ / 98 МБ, пауза 127us всего 444,411 мс
Пропущено 153 кадра! Приложение может выполнять слишком много работы над своим основным потоком.
Кто-нибудь может предложить лучший подход для моей ситуации? Действительно ли слишком много работы - генерировать в 81 раз сетку 3х3?
Спасибо
Редактировать
Так что теперь я попытался вручную записать файл xml. Я думал, что отсутствие необходимости много раз надувать xml улучшит ситуацию, но это не так. У меня все еще остается та же проблема «Слишком много работы в главном потоке», просто загружая файл XML.
<TableLayout
android:id="@+id/sudokuGrid"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:divider="@drawable/row_divider"
android:showDividers="middle"
android:shrinkColumns="*"
android:stretchColumns="*"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/solverTitle">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/column_divider"
android:showDividers="middle">
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<include
layout="@layout/cell_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
А вот мой макет ячейки, на случай, если кто-то захочет попробовать.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cellValue"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/pencilOne"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="1"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilFour"
app:layout_constraintEnd_toStartOf="@+id/pencilTwo"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/pencilTwo"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="2"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilFive"
app:layout_constraintEnd_toStartOf="@+id/pencilThree"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilOne"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/pencilThree"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="3"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilSix"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilTwo"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/pencilFour"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="4"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilSeven"
app:layout_constraintEnd_toStartOf="@+id/pencilFive"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pencilOne" />
<TextView
android:id="@+id/pencilSix"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="6"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilNine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilFive"
app:layout_constraintTop_toBottomOf="@+id/pencilThree" />
<TextView
android:id="@+id/pencilSeven"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="7"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/pencilEight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pencilFour" />
<TextView
android:id="@+id/pencilEight"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="8"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/pencilNine"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilSeven"
app:layout_constraintTop_toBottomOf="@+id/pencilFive" />
<TextView
android:id="@+id/pencilNine"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="9"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilEight"
app:layout_constraintTop_toBottomOf="@+id/pencilSix" />
<TextView
android:id="@+id/pencilFive"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:text="5"
android:textSize="10sp"
android:gravity="center"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pencilEight"
app:layout_constraintEnd_toStartOf="@+id/pencilSix"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/pencilFour"
app:layout_constraintTop_toBottomOf="@+id/pencilTwo" />
</android.support.constraint.ConstraintLayout>