-
Notifications
You must be signed in to change notification settings - Fork 19
ДЗ по RecyclerView #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aluminiy
wants to merge
2
commits into
Otus-Android:master
Choose a base branch
from
Aluminiy:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/ru/otus/cryptosample/coins/feature/adapter/CarouselAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package ru.otus.cryptosample.coins.feature.adapter | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.recyclerview.widget.DiffUtil | ||
| import androidx.recyclerview.widget.ListAdapter | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import ru.otus.cryptosample.coins.feature.CoinState | ||
| import ru.otus.cryptosample.databinding.ItemCoinBinding | ||
|
|
||
| class CarouselAdapter: ListAdapter<CoinState, CoinViewHolder>(CarouselDiff) { | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinViewHolder { | ||
| val binding = ItemCoinBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
| val params = binding.root.layoutParams | ||
| ?: RecyclerView.LayoutParams( | ||
| RecyclerView.LayoutParams.WRAP_CONTENT, | ||
| RecyclerView.LayoutParams.WRAP_CONTENT | ||
| ) | ||
| val screenWidth = parent.resources.displayMetrics.widthPixels | ||
| params.width = screenWidth / 2 - 16 | ||
|
|
||
| binding.root.layoutParams = params | ||
|
|
||
| return CoinViewHolder(binding) | ||
| } | ||
|
|
||
| override fun getItemViewType(position: Int): Int = CoinsAdapter.VIEW_TYPE_COIN | ||
|
|
||
| override fun onBindViewHolder( | ||
| holder: CoinViewHolder, | ||
| position: Int | ||
| ) { | ||
| holder.bind(getItem(position)) | ||
| } | ||
|
|
||
| override fun onBindViewHolder( | ||
| holder: CoinViewHolder, | ||
| position: Int, | ||
| payloads: List<Any> | ||
| ) { | ||
| holder.bind(getItem(position), payloads) | ||
| } | ||
| } | ||
|
|
||
| object CarouselDiff : DiffUtil.ItemCallback<CoinState>() { | ||
|
|
||
| override fun areItemsTheSame(old: CoinState, new: CoinState) = old.id == new.id | ||
|
|
||
| override fun areContentsTheSame(old: CoinState, new: CoinState) = old == new | ||
|
|
||
| override fun getChangePayload(oldItem: CoinState, newItem: CoinState): Any? = | ||
| if (oldItem.highlight != newItem.highlight) { | ||
| Payload.HOT_MOVER_CHANGED | ||
| } else null | ||
|
|
||
| object Payload { | ||
| const val HOT_MOVER_CHANGED = "HOT_MOVER_CHANGED" | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
app/src/main/java/ru/otus/cryptosample/coins/feature/adapter/CarouselViewHolder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ru.otus.cryptosample.coins.feature.adapter | ||
|
|
||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import ru.otus.cryptosample.coins.feature.CoinState | ||
| import ru.otus.cryptosample.databinding.ItemCarouselBinding | ||
|
|
||
| class CarouselViewHolder( | ||
| binding: ItemCarouselBinding, | ||
| sharedPool: RecyclerView.RecycledViewPool | ||
| ): RecyclerView.ViewHolder(binding.root) { | ||
|
|
||
| private val carouselAdapter = CarouselAdapter() | ||
|
|
||
| init { | ||
| binding.recyclerView.apply { | ||
| layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) | ||
| adapter = carouselAdapter | ||
| setRecycledViewPool(sharedPool) | ||
| isNestedScrollingEnabled = false | ||
| setHasFixedSize(true) | ||
| itemAnimator = ItemAnimator() | ||
| } | ||
| } | ||
|
|
||
| fun bind(coins: List<CoinState>) { | ||
| carouselAdapter.submitList(coins) | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/ru/otus/cryptosample/coins/feature/adapter/CoinDiff.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package ru.otus.cryptosample.coins.feature.adapter | ||
|
|
||
| import androidx.recyclerview.widget.DiffUtil | ||
| import ru.otus.cryptosample.coins.feature.CoinState | ||
|
|
||
| data class Carousel(val categoryId: String, val coins: List<CoinState>) | ||
|
|
||
| object CoinDiff : DiffUtil.ItemCallback<CoinsAdapterItem>() { | ||
|
|
||
| override fun areItemsTheSame(oldItem: CoinsAdapterItem, newItem: CoinsAdapterItem): Boolean = | ||
| when { | ||
| oldItem is CoinsAdapterItem.CategoryHeader && | ||
| newItem is CoinsAdapterItem.CategoryHeader -> | ||
| oldItem.categoryName == newItem.categoryName | ||
|
|
||
| oldItem is CoinsAdapterItem.CoinItem && | ||
| newItem is CoinsAdapterItem.CoinItem -> | ||
| oldItem.coin.id == newItem.coin.id | ||
|
|
||
| oldItem is CoinsAdapterItem.Carousel && | ||
| newItem is CoinsAdapterItem.Carousel -> | ||
| oldItem.categoryId == newItem.categoryId | ||
|
|
||
| else -> false | ||
| } | ||
|
|
||
| override fun areContentsTheSame(oldItem: CoinsAdapterItem, newItem: CoinsAdapterItem): Boolean = | ||
| oldItem == newItem | ||
|
|
||
| override fun getChangePayload(oldItem: CoinsAdapterItem, newItem: CoinsAdapterItem): Any? { | ||
| return when (oldItem) { | ||
| is CoinsAdapterItem.CoinItem -> { | ||
| val oldCoin = oldItem.coin | ||
| val newCoin = (newItem as CoinsAdapterItem.CoinItem).coin | ||
| if (oldCoin.highlight != newCoin.highlight) { | ||
| Payload.HOT_MOVER_CHANGED | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| else -> null | ||
| } | ||
| } | ||
|
|
||
| object Payload { | ||
| const val HOT_MOVER_CHANGED = "HOT_MOVER_CHANGED" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут 16 это пиксели и они зависят от плотности, лучше переводить все в
dp