앱 만들기/안드로이드

스크롤 뷰 만들기 와 주의사항

나도 처음이야 2022. 1. 27.

화면에 넘치는 데이터를 처리하는 것 중 가장 기본적인 것이 바로 스크롤 뷰이다. 

스크롤 뷰에서 주의 사항은 다음과 같다.

1. 스크롤 뷰의 자식은 하나만 생성한다.

2. 스크롤 뷰 내부의 layout_height 속성은 wrap_content 이어야 성능상 좋다.

 

이 두가지를 기반으로 코드를 작성하면 하기와 같다.

1. Xml 화면 구성

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView1"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView2"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView3"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView4"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView5"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView6"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView7"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView8"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView9"
                android:textSize="70dp"/>

            <TextView
                android:id="@+id/textView10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView10"
                android:textSize="70dp"/>

        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

부모의 레이아웃은 중요하지 않다. 스크롤 뷰가 하나의 자식을 가지고, 그 자식의 height 속성이 wrap_content 야 한다는 것이 주요 포인트이다.

 

2. 자바코드에서는 단순히 해당 xml 화면을 로드 한다.

public class MainActivity extends AppCompatActivity {

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

 

스크롤 뷰는 간단하지만, 단점이 있다.

많은 콘텐츠를 한번에 로드하기에 메모리 이슈가 발생하여 앱이 죽을 수 있다. (메모리 부족)

그렇기에 반복되는 콘텐츠(이미지 etc) 등을 사용시에는 ListView 혹은 GridView 를 사용해야 한다.

감사합니다

 

https://soo0100.tistory.com/1716

 

안드로이드 어댑터 뷰 기초(리스트 뷰 만들기)

https://soo0100.tistory.com/1713 스크롤 뷰 만들기 와 주의사항 화면에 넘치는 데이터를 처리하는 것 중 가장 기본적인 것이 바로 스크롤 뷰이다. 스크롤 뷰에서 주의 사항은 다음과 같다. 1. 스크롤 뷰

soo0100.tistory.com

 

반응형

댓글