-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request a new layout if previous smooth scroller was active during la…
…yout
- Loading branch information
1 parent
cc945e4
commit 3f2940f
Showing
7 changed files
with
197 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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
153 changes: 153 additions & 0 deletions
153
...com/rubensousa/dpadrecyclerview/sample/ui/screen/animation/PredictiveAnimationFragment.kt
This file contains 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,153 @@ | ||
package com.rubensousa.dpadrecyclerview.sample.ui.screen.animation | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.rubensousa.dpadrecyclerview.OnViewHolderSelectedListener | ||
import com.rubensousa.dpadrecyclerview.compose.DpadComposeFocusViewHolder | ||
import com.rubensousa.dpadrecyclerview.sample.R | ||
import com.rubensousa.dpadrecyclerview.sample.databinding.ScreenRecyclerviewBinding | ||
import com.rubensousa.dpadrecyclerview.sample.ui.model.ListTypes | ||
import com.rubensousa.dpadrecyclerview.sample.ui.viewBinding | ||
import com.rubensousa.dpadrecyclerview.sample.ui.widgets.common.MutableListAdapter | ||
import com.rubensousa.dpadrecyclerview.sample.ui.widgets.common.PlaceholderComposable | ||
import com.rubensousa.dpadrecyclerview.sample.ui.widgets.item.ItemComposable | ||
import com.rubensousa.dpadrecyclerview.sample.ui.widgets.item.MutableGridAdapter | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class PredictiveAnimationFragment : Fragment(R.layout.screen_recyclerview) { | ||
|
||
private val binding by viewBinding(ScreenRecyclerviewBinding::bind) | ||
private val viewModel by viewModels<PredictiveAnimationViewModel>() | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val itemAdapter = PredictiveItemAdapter() | ||
binding.recyclerView.apply { | ||
adapter = itemAdapter | ||
addOnViewHolderSelectedListener(object : OnViewHolderSelectedListener { | ||
override fun onViewHolderSelected( | ||
parent: RecyclerView, | ||
child: RecyclerView.ViewHolder?, | ||
position: Int, | ||
subPosition: Int, | ||
) { | ||
viewModel.loadMore(position) | ||
} | ||
}) | ||
requestFocus() | ||
} | ||
viewModel.getItems().observe(viewLifecycleOwner) { items -> | ||
itemAdapter.submitList(items) | ||
} | ||
} | ||
|
||
class PredictiveItemAdapter( | ||
) : MutableListAdapter<Int, DpadComposeFocusViewHolder<Int>>(MutableGridAdapter.DIFF_CALLBACK) { | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): DpadComposeFocusViewHolder<Int> { | ||
return when (viewType) { | ||
ListTypes.ITEM -> { | ||
DpadComposeFocusViewHolder(parent) { item -> | ||
ItemComposable( | ||
modifier = Modifier.fillMaxWidth().height(200.dp), | ||
item = item, | ||
) | ||
} | ||
} | ||
|
||
else -> { | ||
DpadComposeFocusViewHolder( | ||
parent, | ||
isFocusable = false | ||
) { | ||
PlaceholderComposable( | ||
Modifier.fillMaxWidth().height(300.dp), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: DpadComposeFocusViewHolder<Int>, position: Int) { | ||
val item = getItem(position) | ||
holder.setItemState(item) | ||
holder.itemView.contentDescription = item.toString() | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
val item = getItem(position) | ||
return if (item >= 0) { | ||
ListTypes.ITEM | ||
} else { | ||
ListTypes.LOADING | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
class PredictiveAnimationViewModel : ViewModel() { | ||
|
||
private val totalItems = 10 | ||
private var offset = 0 | ||
private var isLoadingMore = false | ||
private val liveData = MutableLiveData<MutableList<Int>>() | ||
private val dispatcher = Dispatchers.Default | ||
|
||
init { | ||
loadFirstPage() | ||
} | ||
|
||
fun getItems(): LiveData<MutableList<Int>> = liveData | ||
|
||
private fun loadFirstPage() { | ||
viewModelScope.launch(dispatcher) { | ||
val newList = mutableListOf<Int>() | ||
repeat(totalItems) { | ||
newList.add(it) | ||
} | ||
liveData.postValue(newList.toMutableList()) | ||
offset = totalItems | ||
} | ||
} | ||
|
||
fun loadMore(selectedPosition: Int) { | ||
if (isLoadingMore) { | ||
return | ||
} | ||
if (selectedPosition < offset - 2) { | ||
return | ||
} | ||
isLoadingMore = true | ||
viewModelScope.launch(dispatcher) { | ||
val currentList = liveData.value!! | ||
val loadingList = currentList + mutableListOf(-1) | ||
liveData.postValue(loadingList.toMutableList()) | ||
delay(5000L) | ||
|
||
val newList = MutableList(totalItems + offset) { it } | ||
liveData.postValue(newList) | ||
offset = newList.size | ||
isLoadingMore = false | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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.