From 8cd2ffc16d1fd6d65089223bb0b366130991055c Mon Sep 17 00:00:00 2001 From: JUNGGWAN KIM Date: Tue, 15 Oct 2024 18:16:32 +0900 Subject: [PATCH] weather scroll --- app/build.gradle.kts | 1 + .../rasel/lunar/launcher/home/LauncherHome.kt | 37 ++++++-- .../launcher/home/adapters/WeatherAdapter.kt | 89 ++++++++++++++++--- .../home/adapters/WeatherHourlyAdater.kt | 6 +- .../launcher/model/WeatherInfoManager.kt | 12 +++ .../main/res/layout/item_hourly_weather.xml | 4 +- app/src/main/res/layout/weather_book.xml | 2 +- 7 files changed, 126 insertions(+), 25 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7c55dbd..46eca59 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -97,6 +97,7 @@ dependencies { implementation("com.squareup.retrofit2:converter-gson:2.6.4") implementation("com.squareup.retrofit2:converter-scalars:2.6.4") implementation("androidx.viewpager2:viewpager2:1.0.0") + implementation("com.squareup.picasso:picasso:2.71828") // implementation ("me.everything:providers-android:1.0.1") // implementation ("me.everything:providers-core:1.0.1") // implementation ("androidx.window:window:1.0.0") diff --git a/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt b/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt index 7f2309f..921cdc2 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt @@ -219,6 +219,25 @@ internal class LauncherHome : Fragment() { binding.infoList.adapter = mRssAdapter binding.notiList.adapter = mNotiAdapter binding.noticeSummary.weatherViewPager.adapter = mWeatherAdapter + binding.noticeSummary.weatherViewPager.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + // 페이지가 변경될 때 호출됨 + mWeatherAdapter?.syncScroll(position) + + } + + override fun onPageScrollStateChanged(state: Int) { + super.onPageScrollStateChanged(state) + // 스크롤 상태가 변경될 때 호출됨 + } + + override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { + super.onPageScrolled(position, positionOffset, positionOffsetPixels) + // 페이지 스크롤이 발생할 때 호출됨 + + } + }) binding.favAppsGroup.setOnClickListener { searchData() } try{binding.mainList.removeOnScrollListener(onScrChanged)}catch (e : Exception){e.printStackTrace()} @@ -315,16 +334,16 @@ internal class LauncherHome : Fragment() { }.find().let {hours -> Handler(Looper.getMainLooper()).post { weatherDressAdapter?.let { - it.update( - mutableListOf().apply { - this.addAll( + it.update( + mutableListOf().apply { + this.addAll( // it.filter(hours) - hours - ) - } - ) - it.notifyDataSetChanged() - } + hours + ) + } + ) + it.notifyDataSetChanged() + } weatherHourlyAdapter?.let { it.update( mutableListOf().apply { diff --git a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt index 3766afd..7fddfb9 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt @@ -1,13 +1,18 @@ package rasel.lunar.launcher.home.adapters import android.annotation.SuppressLint +import android.os.Handler +import android.os.Looper import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.RecyclerView import rasel.lunar.launcher.R import rasel.lunar.launcher.databinding.WeatherBookBinding +import rasel.lunar.launcher.utils.BLog class WeatherAdapter( private val pages: List, @@ -17,26 +22,86 @@ class WeatherAdapter( class PageViewHolder(val view: View): RecyclerView.ViewHolder(view) + + var childs : ArrayList = arrayListOf() + + @SuppressLint("ResourceType") override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder { - return PageViewHolder(LayoutInflater.from(parent.context).inflate(pages[viewType], parent, false)) + + + val layoutInflater = LayoutInflater.from(parent.context).inflate(pages[viewType], parent, false) +// var first = layoutInflater.findViewById(R.id.recycler_hourly_weather) +// var second = layoutInflater.findViewById(R.id.weather_dress_recycller) + + return PageViewHolder(layoutInflater) } + + fun syncScroll(newPosition: Int) { + childs[newPosition].let {recyclerView -> + BLog.LOGE("recyclerView >>> ${recyclerView} 1 ") + if (!isSyncingScroll) { + isSyncingScroll = true + BLog.LOGE("recyclerView >>> ${recyclerView} 2 ") + childs.forEach { c -> +// val visibleItemCount = (layoutManager?.findLastVisibleItemPosition() ?: 0) - (layoutManager?.findFirstVisibleItemPosition() ?: 0) + if (c != recyclerView) { + (c.layoutManager as LinearLayoutManager)?.let { + it.findFirstVisibleItemPosition()?.let { + (recyclerView?.layoutManager as? LinearLayoutManager)?.let { target -> + target.scrollToPositionWithOffset(it, 0) + } +// .scrollToPosition(it) + } + } + BLog.LOGE("recyclerView >>> ${recyclerView} 3 ") + } + } + isSyncingScroll = false + BLog.LOGE("recyclerView >>> ${recyclerView} 4 ") + } + } + + } + + + val mOnScrollListener = object : RecyclerView.OnScrollListener() { + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) +// recyclerView.postDelayed({syncScroll(recyclerView)},10L) +// recyclerView.post{syncScroll(recyclerView)} + } + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + } + } + var isSyncingScroll = false override fun onBindViewHolder(holder: PageViewHolder, position: Int) { if (adatpers[position] != null) { - when (position) { - 0 -> holder.view.findViewById(R.id.recycler_hourly_weather).apply { - this.adapter = adatpers[position] - this.layoutManager = - LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) - } - - else -> holder.view.findViewById(R.id.weather_dress_recycller).apply { - this.adapter = adatpers[position] - this.layoutManager = - LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) + (holder.view as RecyclerView).apply { + this.adapter = adatpers[position] + this.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) + if (!childs.contains(this)) { + childs.add(this) + this.addOnScrollListener(mOnScrollListener) + LinearSnapHelper().attachToRecyclerView(this) } } +// when (position) { +// 0 -> holder.view.findViewById(R.id.recycler_hourly_weather).apply { +// this.adapter = adatpers[position] +// this.layoutManager = +// +// } +// +// else -> holder.view.findViewById(R.id.weather_dress_recycller).apply { +// this.adapter = adatpers[position] +// this.layoutManager = +// LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) +// } +// } } } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherHourlyAdater.kt b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherHourlyAdater.kt index c8bc136..f5a37c1 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherHourlyAdater.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherHourlyAdater.kt @@ -5,7 +5,9 @@ import android.graphics.Color import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.net.toUri import androidx.recyclerview.widget.RecyclerView +import com.squareup.picasso.Picasso import rasel.lunar.launcher.databinding.ItemHourlyWeatherBinding import rasel.lunar.launcher.databinding.ItemRecHourlyDressBinding import rasel.lunar.launcher.model.Hour @@ -26,6 +28,9 @@ class WeatherHourlyAdapter(private val dataSet: ArrayList): RecyclerView.A data?.let { WeatherInfoManager.getShowingInfo(it).apply { holder.viewItem.setInfo(this) + Picasso.get() + .load(this.urlImgWeather) + .into(holder.viewItem.imgWeather) } BLog.LOGE("reeeeeeeeeee >>> ${holder.viewItem.hour.text}") holder.viewItem.amOrPm.visibility = @@ -41,7 +46,6 @@ class WeatherHourlyAdapter(private val dataSet: ArrayList): RecyclerView.A this@apply.isSelected = false } } - holder.viewItem.imgDress.setImageLevel(it.temp_c.toInt()) } } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt index 9741fa2..efc5f81 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt @@ -18,6 +18,8 @@ class ShowingWeatherInfo() { var dress: String? = null var imgDress: Int? = null var temp: String? = null + var textCondition: String? = null + var urlImgWeather: String? = null fun setLocation(loc: Location?): ShowingWeatherInfo { textLocation = "${loc?.name ?: "도시"} / ${loc?.country ?: "나라"}" @@ -28,6 +30,8 @@ class ShowingWeatherInfo() { setTextHour(hour) setDress(hour) setTemp(hour) + setTextCondition(hour) + setUrlImgWeather(hour) return this } private fun setAmOrPm(hour: Hour) = WeatherInfoManager.toZonedDateTime(hour.time_epoch).hour.run { @@ -69,6 +73,14 @@ class ShowingWeatherInfo() { } } private fun setTemp(hour: Hour) {temp = "${hour.temp_c}도"} + + private fun setTextCondition(hour: Hour) {textCondition = hour.condition?.text} + + private fun setUrlImgWeather(hour: Hour) { + hour.condition?.icon?.let { + urlImgWeather = if (!it.contains("https:")) "https:$it" else it + } + } } object WeatherInfoManager { diff --git a/app/src/main/res/layout/item_hourly_weather.xml b/app/src/main/res/layout/item_hourly_weather.xml index 25ebaf1..2ea68bb 100644 --- a/app/src/main/res/layout/item_hourly_weather.xml +++ b/app/src/main/res/layout/item_hourly_weather.xml @@ -29,7 +29,7 @@ android:text="@{info.textHour}" android:textAlignment="center"/>