findVIewById에 관하여

조회수 3639회

안녕하세요 안드로이드 코딩을 하다가 갑자기 궁금한 것이 생겨서 질문올려요!

이미지

그림을 보시면

작은 네모( 파란색으로 구분이 되어있는)들이 총 16개가 있는데 이 것들은 모두 TextView입니다. 그리고 빨간색 네모는 LinearLayout으로 구분한 것입니다.

빨간색 네모(총 4개)끼리는 연관이 없지만, 빨간색 네모 안에 있는 파란색 네모 4개끼리는 나름 연관이 있습니다.

이 화면을 하나의 Activity xml 파일 안에 전부 다 넣으면 복잡할 것 같고, 16개의 TextView에 id를 하나씩 부여하기에는 너무 많고 하여 밑의 코드처럼 include를 사용해서 파일을 분리시키고 id의 개수도 줄였습니다.

[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  //라인 구분을 위한 뷰
        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  //라인 구분을 위한 뷰
        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>

XML코드는 이런식으로 하였습니다.

이제 JAVA 코드로 가서

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

이런식의 참조를 하였습니다.

저 코드를 실행해 보면 화면에는

이미지

이렇게 나오게 됩니다.

데이터 들의 연관성들을 생각해서 저러한 3차원 배열 구조를 생각하게 되었습니다.

그런데 지금 findViewById를 코드상으로 적은 것은 3번 뿐이지만, for문을 돌다보면 빨간 네모(z), 세로(x), 텍스트뷰(y) 이렇게 해서 총 28번을 부르게 됩니다.

findViewById가 성능상으로 않좋다는 것을 들어본적이 있는데, 혹시 저러한 뷰 배치를 다르게 할 방법이 있을까요? 3차원 배열은 제가 xml코드나 java코드를 조금 줄여보고자 저렇게 한 것이므로 다른 방법이 있다면 추천 부탁드립니다!

또한 제가 지금 TextView들을 setView() 메소드를 벗어나면 참조를 할 수가 없는 형태로 되어있는데, 저 16개의 TextView의 텍스트 전체를 바꿀 일이 있다면 setView() 메소드를 다시 부르게 됩니다. 즉 findViewById를 다시 하는거지요.... 이게 굉장히 않좋다는 것은 알고 있는데, TextView를 TextView[][][] text = new TextView[4][ 2][ 2]; 이렇게 하면 메모리 상으로 좋지 않을 것 같아서 이렇게 하지 않았습니다.

findViewById를 다시 하는 것과 TextView[ 4 ][ 2][ 2]를 가지고 있는 것 어떤 것이 더 좋은 방법 일까요?

질문이 길어졌습니다...ㅠ 죄송합니다.. ㅠㅠ

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 제목을 제대로 쓴다는 것을 깜빡하고 그냥 올렸었네요.. 제목 수정하였습니다. 알 수 없는 사용자 2016.5.19 16:24

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)