Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application doing too much work in main thread loading the xml layout file

I am making a 9x9 Sudoku grid where each of the 81 cells is itself a 3x3 grid. A single cell looks something like this:

1 2 3

4 5 6

7 8 9

Each number represents pencil annotations for that cell. I have a file called cell_layout.xml representing this 3x3 arrangement.

I am already able to generate the grid, and the code works:

    @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);
        }

    }

The code above just inflates 81 times the wanted layout into the TableLayout.

It works, so what is your problem?

The activity takes too long to create. Even if I test with just one row of the grid, it takes too long for the method to inflate many times the wanted layout.

I get:

Background concurrent copying GC freed 131244(9MB) AllocSpace objects, 0(0B) LOS objects, 24% free, 74MB/98MB, paused 127us total 444.411ms

Skipped 153 frames! The application may be doing too much work on its main thread.

Can anyone suggest a better approach for my situation? Is really too much work generating 81 times a 3x3 grid?

Thanks

Edit

So I now tried to manually write down the xml file. I thought that not having to inflate many times the xml would improve things, but it didn´t. I still have the same problem "Too much work in the main thread" just by loading the xml file.

<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>

And here is my cell layout, in case someone wants to give it a try.

<?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>
like image 227
PhysicsPDF Avatar asked Sep 29 '19 16:09

PhysicsPDF


People also ask

How can we increase performance of application using threading?

With scenarios like these, we suggest that your app not include explicit references to UI objects in threaded work tasks. Avoiding such references helps you avoid these types of memory leaks, while also steering clear of threading contention. In all cases, your app should only update UI objects on the main thread.

What causes AN ANR?

Application Not Responding (ANR) errors are triggered when the UI thread of the application is not responding for more than 5 seconds. You can read more about ANRs and diagnosing ANRs in the Android documentation. Additionally, Crashlytics can help pinpoint specific problematic threads.

What is deadlock in Android?

Deadlocks. A deadlock occurs when a thread enters a waiting state because a required resource is held by another thread, which is also waiting for a resource held by the first thread. If the app's main thread is in this situation, ANRs are likely to happen.

How to detect ANR in Android?

A good way to try to detect the problem is by fetching the file /data/anr/traces. txt which is generated after a ANR happens on a device (beware that it is overridden after another ANR happens). That offers you a overview of what each thread was doing at the time of the ANR.


1 Answers

For more information about Application doing too much work in main thread Please refer this post

https://stackoverflow.com/a/21126690/7666442

You should use RecyclerView with GridLayoutManager

Follow this steps

First add below dependencies in your Build.Gradle file to use RecyclerView

implementation 'com.google.android.material:material:1.0.0-beta01'

Note : no need to add extra dependencies of RecyclerView if you have already added dependencies of com.google.android.material:material

Now add RecyclerView in your activity file layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".JavaActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/myRecyclerView"
        android:layout_height="wrap_content" />

</LinearLayout>

Now you need to use GridLayoutManager to display your list item as grid

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class JavaActivity extends AppCompatActivity {

    RecyclerView myRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java);

        myRecyclerView = findViewById(R.id.myRecyclerView);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(JavaActivity.this, 3);
        myRecyclerView.setLayoutManager(gridLayoutManager);
        myRecyclerView.setAdapter(new MyAdapter(this));
    }

}

Create a MyAdapter class like this

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_list_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvText.setText(String.valueOf(position + 1));
        if (position % 2 == 0) {
            holder.imgBanner.setBackgroundColor(Color.RED);
        } else {
            holder.imgBanner.setBackgroundColor(Color.GREEN);
        }
    }

    @Override
    public int getItemCount() {
        return 81;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imgBanner;
        TextView tvText;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imgBanner = itemView.findViewById(R.id.imgBanner);
            tvText = itemView.findViewById(R.id.tvText);
        }
    }
}

row_list_item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imgBanner"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/tvText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:textStyle="bold" />
</RelativeLayout>

OUTPUT

enter image description here

like image 60
AskNilesh Avatar answered Oct 06 '22 00:10

AskNilesh