728x90
반응형
레이아웃 추가
검색 기능을 추가하기 전에 몇 개의 노출시킬 카드 아이템을 만들려고 한다
아이템 내용은 임시로 하드코딩하려고 한다
아이템 레이아웃 생성 (item_city_card.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<!-- 도시 이름 -->
<TextView
android:id="@+id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="서울"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<!-- 날씨 아이콘 -->
<ImageView
android:id="@+id/weatherIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/sunny_128"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- 현재 온도 -->
<TextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="18°C"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/cityName"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
아이템을 담을 리사이클러뷰 추가 (activity_main.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">
<!-- 검색 결과 리스트 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_city_card" />
</androidx.constraintlayout.widget.ConstraintLayout>
데이터 클래스 생성 (CityWeatherInfo)
package com.example.weatherapp.ui.model
data class CityWeatherInfo (
val cityName: String, // 도시 이름
val weatherIconResId: Int, // 날씨 아이콘 리소스 ID
val temperature: String // 온도 정보
)
어댑터 생성 (CityWeatherAdapter)
package com.example.weatherapp.ui.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.weatherapp.R
import com.example.weatherapp.ui.model.CityWeatherInfo
class CityWeatherAdapter (private var cityList: List<CityWeatherInfo>) :
RecyclerView.Adapter<CityWeatherAdapter.CityViewHolder>() {
inner class CityViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val cityName: TextView = itemView.findViewById(R.id.cityName)
val weatherIcon: ImageView = itemView.findViewById(R.id.weatherIcon)
val temperature: TextView = itemView.findViewById(R.id.temperature)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CityViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_city_card, parent, false)
return CityViewHolder(view)
}
override fun onBindViewHolder(holder: CityViewHolder, position: Int) {
val cityWeather = cityList[position]
holder.cityName.text = cityWeather.cityName
holder.weatherIcon.setImageResource(cityWeather.weatherIconResId)
holder.temperature.text = cityWeather.temperature
}
override fun getItemCount() = cityList.size
// 데이터 업데이트 기능
fun updateList(newList: List<CityWeatherInfo>) {
cityList = newList
notifyDataSetChanged()
}
}
액티비티에서 리스트 설정 (SearchActiviy)
package com.example.weatherapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.weatherapp.ui.adapter.CityWeatherAdapter
import com.example.weatherapp.ui.model.CityWeatherInfo
class SearchActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: CityWeatherAdapter
private val cityWeatherList = mutableListOf<CityWeatherInfo>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
recyclerView = findViewById(R.id.recyclerView_search)
// RecyclerView 설정
adapter = CityWeatherAdapter(cityWeatherList)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
// 데이터 추가
loadCityWeatherData()
}
private fun loadCityWeatherData() {
cityWeatherList.add(CityWeatherInfo("현재 위치", R.drawable.sunny_128, "22°C"))
cityWeatherList.add(CityWeatherInfo("서울", R.drawable.cloudy_128, "18°C"))
cityWeatherList.add(CityWeatherInfo("부산", R.drawable.rainy_128, "20°C"))
cityWeatherList.add(CityWeatherInfo("제주", R.drawable.sunny_128, "25°C"))
cityWeatherList.add(CityWeatherInfo("대구", R.drawable.cloudy_128, "21°C"))
// Adapter에 데이터 변경 알림
adapter.updateList(cityWeatherList)
}
}
결과
728x90
반응형
'간단한 안드로이드 앱 만들기 (날씨 앱)' 카테고리의 다른 글
13. 날씨 데이터 리스트화 (0) | 2025.02.26 |
---|---|
14. 옵저버 오남용 수정 (0) | 2025.02.25 |
11. 도시 검색 버튼 추가 (1) | 2025.02.21 |
10. 지도 위에 날씨 이미지 표시하기 (카카오맵 라벨 생성) (0) | 2025.02.21 |
9. 지도 나타내기 (카카오맵 API) (0) | 2025.02.20 |