About findVIewById

Asked 2 years ago, Updated 2 years ago, 22 views

Hello, I was coding for Android and I suddenly got a question, so I'm posting a question!

If you look at the picture,

There are 16 small squares in total, all of which are TextViews. And the red square is separated by Linear Layout.

Red squares are not related to each other, but the four blue squares in the red squares are related to each other.

It would be complicated to put all of this screen in one Activity xml file, and it was too much to give each of the 16 TextViews an id, so I separated the file using include like the code below and reduced the number of ids.

[horizontal.xml]

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_horizontal_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

    <View //View for line separation
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#ccc" />

    <TextView
        android:id="@+id/text_horizontal_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" />

</LinearLayout>

[vertical.xml]

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
       android:id="@+id/vertical_1"
       layout="@layout/horizontal"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1" />

    <View //View for line separation
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ccc" />

    <include
       android:id="@+id/vertical_2"
       layout="@layout/horizontal"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1" />

</LenearLayout>

[content_main.xml]

<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:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <include
            android:id="@+id/main_11"
            layout="@layout/vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000" />

        <include
            android:id="@+id/main_12"
            layout="@layout/vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <include
            android:id="@+id/main_21"
            layout="@layout/vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000" />

        <include
            android:id="@+id/main_22"
            layout="@layout/vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

This is how the XML code works.

Now go to the JAVA code

[MainAcitivity.java]

public static final int[] main_id = { R.id.main_11, R.id.main_12,  R.id.main_21, R.id.main_22 }
public static final int[] vertical_id = { R.id.vertical_1, R.id.vertical_2 }
public static final int[] text_id = { R.id.text_horizontal_1, R.id.text_horizontal_2 }

View[] main_2by2 = new View[main_id.length];

public void setView() {

    for ( int z = 0 ; z  < main_id.length ; z++ ) {
        main_2by2[z] = findViewById(main_id[z]);
        for ( int x = 0 ; x < vertical_id.length ; x++ ) {
            View[] vertical = new View[vertical_id.length];
            vertical[x] = main_2by2[z].findViewById(vertical_id[x]);
            for ( int y = 0 ; y < text_id.length ; y++ ) {
                TextView[] text = new TextView[text_id.length];
                text[y] = (TextView) vertical[x].findViewById(text_id[y]);
                text[y].setText( z + "," + x + "," + y );
            }
        }
    }
}

I made a reference like this reference.

If you run that code, the screen will show you

It comes out like this.

I thought about those three-dimensional arrangements because of the associations of the data.

But now, I only wrote findViewById in code three times, but when I look around the for door, I call it 28 times in total, like red square (z), vertical (x), and text view (y).

I've heard that findViewById is not good in terms of performance, is there a way to arrange such views differently? I did it like that to reduce the xml code or java code a little bit, so if there is another way, please recommend it!

Also, I can't refer to TextViews if I go outside of the setView() method right now, but if I have to change the entire text of those 16 TextViews, I'll call the setView() method again. That is to say, do findViewById again... I know this is really bad, but TextView TextView[][][][] text = new TextView[4][2]; I didn't do this because I thought it wouldn't be good for memory.

Which is the better way to do findViewById again or to have TextView[4][2][2]?

The question is getting longer...I'm sorry. ㅠ<

android

2022-09-22 12:50

1 Answers

It provides widgets for configuring grid-style views on Android. It is also a way to configure the layout directly like the code you posted, but using the API provided by Android will have advantages in terms of time cost, readability, performance, and maintenance.

GridLayout

GridView

RecyclerView(GridLayoutManager)

Flexbox Layout (recently released)

And I think the call of findViewById may or may not be a problem for performance. In ListView and RecyclerView, it is true that ViewHolder patterns exist to avoid such problems. However, the criteria themselves can become ambiguous if the views you implement are exposed to are fixed, frequent, or not updated frequently. Rather, spreading nested view structures flat (flat) may be more effective for rendering performance, or creating a single custom view containing a grid can also be an option.

I left a link because there was a question and answer related to findViewById in Hashcode.

http://hashcode.co.kr/questions/1574/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C%EC%97%90%EC%84%9C-findviewbyid-%ED%95%A8%EC%88%98%EA%B0%80-%EC%84%B1%EB%8A%A5%EC%9D%B4-%EB%96%A8%EC%96%B4%EC%A7%80%EB%82%98%EC%9A%94


2022-09-22 12:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.