inflearn logo
강의

講義

知識共有

[初級編] Androidコミュニティアプリ作成(Android Kotlin)

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

194

qwerty111222

投稿した質問数 5

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>

 

android kotlin firebase

回答 1

0

bokchi

안녕하세요

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

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

이미지가 기본이미지인지 확인

0

132

1

NavController error 발생

0

141

1

fragment 생성하고 메인에서 불러왔는데 안뜹니다.

0

141

2

67강 댓글

0

100

2

7강 데이터바인딩 에러

0

116

2

Firebase 스토리지 유료화 문제

1

297

2

게시글 이미지가 파이어베이스에 저장되지 않습니다.

0

181

2

AVD 갤러리에 이미지 저장 안되는 문제

0

240

2

이미지 받아오는 방법?

0

204

2

회원탈퇴 기능을 추가하려고 합니다.

0

188

2

상태바 질문 드립니다.

0

111

1

섹션2 인트로 페이지 꾸미기 질문 드립니다.

0

110

1

게시판 글을 길게 쓸경우

0

124

2

로그인 로그아웃

0

177

2

갤럭시 연결시 게시판에 업로드한 사진이 보이지 않아요.

0

207

2

웹뷰 AVD 실행안됨

0

191

1

자막켜기가 안되요 ㅜ.ㅜ

1

185

1

리사이클러뷰, 그리드레이아웃 오류

0

186

2

firebase 스마트폰으로 연결이 안되는데 원인이 있을까요

0

259

2

안드로이드 스튜디오 게시글 이미지 업로드 유무

0

217

1

firebase 설정 오류

0

304

2

홈 화면 커뮤니티

0

178

1

게시판 글 읽기

0

240

2

카테고리 북마크 질문입니다.

0

174

1