인프런 커뮤니티 질문&답변

qwerty111222님의 프로필 이미지

작성한 질문수

[초급편] 안드로이드 커뮤니티 앱 만들기(Android Kotlin)

리사이클러뷰 오류 해결 방법이 궁금합니다.

24.07.23 00:06 작성

·

56

0

개발자님 안녕하세요.

안드로이드 커뮤니티 앱 만들기 섹션 4 팁 페이지 만들기 중 컨텐츠 리스트 만들기를 공부하고 있습니다. 공부하던 중 리사이클러뷰에 오류가 있어서 질문 남깁니다. 오류는 화면에 그리드의 개수만큼만 아이템이 뜹니다. 아이템을 4개를 넣어 놓았을 때, 그리드의 개수가 2개면 아이템 2개만, 그리드 개수가 3개면 아이템 3개만, 4개면 4개만 화면에 뜹니다. 그리드 개수가 5개면 4개만 뜹니다.

관련 코드 아래에 작성해 놓겠습니다. 검토해주시면 정말 감사하겠습니다. 혹시 전체 코드가 필요할 시 바로 구글드라이브로 전달해드리겠습니다!! 매번 감사드립니다.

-ContentsListActivity

package com.example.mysolelife.contentsList

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mysolelife.R

class ContentsListActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contents_list)

        val rv : RecyclerView = findViewById(R.id.rv)

        val items = ArrayList<ContentsModel>()
        items.add(ContentsModel("imageUrl1", "title1"))
        items.add(ContentsModel("imageUrl2", "title2"))
        items.add(ContentsModel("imageUrl3", "title3"))
        items.add(ContentsModel("imageUrl4", "title4"))

        val rvAdapter = ContentsRVAdapter(items)

        rv.adapter = rvAdapter

        rv.layoutManager = GridLayoutManager(this, 2)


    }
}

-ContentsModel

data class ContentsModel (
    var title: String = "",
    val imageUrl : String = ""
)

-ContentsRVAdapter

class ContentsRVAdapter(val items : ArrayList<ContentsModel>) : RecyclerView.Adapter<ContentsRVAdapter.Viewholder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentsRVAdapter.Viewholder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.contents_rv_items, parent, false)
        return Viewholder(v)
    }

    override fun onBindViewHolder(holder: ContentsRVAdapter.Viewholder, position: Int) {
        holder.bindItems(items[position])
    }

    override fun getItemCount(): Int {
        return items.size
    }

    inner class Viewholder(itemView : View) : RecyclerView.ViewHolder(itemView) {
        fun bindItems(item : ContentsModel){
            val contentTitle = itemView.findViewById<TextView>(R.id.textarea)
            contentTitle.text = item.title
        }
    }
}

-activity_contents_list.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".contentsList.ContentsListActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="카테고리 영역 텍스트"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

-contents_rv_items

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".contentsList.ContentsListActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="카테고리 영역 텍스트"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

답변 1

0

개복치개발자님의 프로필 이미지
개복치개발자
지식공유자

2024. 07. 24. 08:33

안녕하세요

전체 프로젝트를 깃허브나 구글 드라이브를 통해서 공유해주시겠어요?

그리고 현재 보고 계신 화면도 스크린샷으로 보내주시면 좋을 것 같습니다.