Search code examples
androidandroid-layoutandroid-custom-view

Custom views are slow


Im building a sudoku and Im facing a performance issue over here.

I have this grid an I want to fill the grid with 81 cells. These cells are custom views, because I want them to have 10 labels in them en some function blabla.

My problem now is that I have to create 81 subviews (no problem), fill them with data from my model (no problem) and then add the whole grid to my layout (biiiig problem).

The whole building is done in a asynctask like this:

protected Void doInBackground(Void... params) {
    Model.Cell[][] sudoku = Controller.getInstance().startNewGame(); //call model for a new game
    ArrayList<TableRow> rows = ga.generateTable(sudoku); //generate the table
    tl = new TableLayout(ga); //create tablelayout
    //add the rows to the layout
    for (TableRow v : rows) {
        tl.addView(v);
    }

    return null;
}

Then after this is done:

protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    RelativeLayout rl = (RelativeLayout) ga.findViewById(R.id.gridContainer); //get the container
    //create layout rules
    android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    //add the table to the container with the layout
    rl.addView(tl, params); //this is slow as hell.

    //stop loading indicator
    this.loading.dismiss();
}

I dont mind get a loading bar for a while. But my loading bar is stopping because rl.addView(tl, params) is superslow.

Can somebody please help me how to keep my main thread fast?


Solution

  • I figured out that a tablelayout in combination with tablerows is very slow. I tried to use Horizontal and VerticalLinearlayouts and this improved the speed of generating the views by 300% at least.

    Thanks for all the new tips. I'll consider them in the future!