android
[kotlin] 리니어레이아웃 xml로 빼기
liz_devel
2021. 8. 4. 09:48
기존코드
> 리니어 레이아웃을 프래그먼트에 정의함
val llm = LinearLayoutManager(cafeActivity)
llm.orientation = LinearLayoutManager.VERTICAL
binding.rvCafe.layoutManager = llm
binding.rvCafe.adapter = textAdapter
변경 코드
> xml로 리니어 레이아웃을 빼줌
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_cafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
android:orientation="vertical"/>
xml로 뺄 때 주의사항
최상단 레이아웃에
"xmlns:app="http://schemas.android.com/apk/res-auto" 포함시켜준다
xml 전체코드 예시
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_cafe"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="16sp"
android:hint="카페 검색어를 입렵해주세요"/>
<Button
android:id="@+id/btn_cafe"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13sp"
android:text="검색" />
<Button
android:id="@+id/btn_cafe_get"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13sp"
android:layout_marginLeft="4dp"
android:text="조회" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_cafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
android:orientation="vertical"/>
</LinearLayout>
app:layoutManager="LinearLayoutManager" < 이 부분을
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" < 이렇게 입력했을 때 나오지 않아서
app:layoutManager="LinearLayoutManager" <이렇게 변경했더니 제대로 작동했다.
RecyclerView 속성 예제
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_membership_point_history"
android:layout_width="match_parent"
android:layout_height="496dp"
app:layoutManager="LinearLayoutManager"
android:orientation="vertical"
app:stackFromEnd="true"
app:reverseLayout="true" />
app:stackFromEnd="true"
app:reverseLayout="true"
둘다 ture로 설정해 줄 경우 아이템 추가 순서 바뀜(아이템 상단으로 쌓임)
false로 할경우 하단에 쌓임
반응형