Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.realwear.uxlibrary_example.horizontalselectorexample

object DataSource {
val arr = arrayOf(
TestColor.Black,
TestColor.Brown,
TestColor.Red,
TestColor.Orange,
TestColor.Yellow,
TestColor.Green,
TestColor.Cyan,
TestColor.Blue,
TestColor.Violet,
TestColor.Gray,
TestColor.White
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,7 @@ class ExampleAdapter(context: Context) :
HorizontalSelectorAdapter<ExampleViewHolder>(context) {
private val weakContext: WeakReference<Context> = WeakReference(context)
private val localTag = ExampleAdapter::class.java.simpleName

private val arr = arrayOf(
TestColor.Black,
TestColor.Brown,
TestColor.Red,
TestColor.Orange,
TestColor.Yellow,
TestColor.Green,
TestColor.Cyan,
TestColor.Blue,
TestColor.Violet,
TestColor.Gray,
TestColor.White
)
private var arr: Array<TestColor> = emptyArray()

override fun onCreateViewHolder(parent: ViewGroup): ExampleViewHolder {
return ExampleViewHolder(
Expand Down Expand Up @@ -105,6 +92,13 @@ class ExampleAdapter(context: Context) :
else -> null
}
}

fun updateArray(array: Array<TestColor>) {
arr = array

// FIXME: When adapter is updated from LiveData items sometimes are not fully inflated
notifyDataSetChanged()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class CustomDialogFragment : DialogFragment() {
)
val view = binding.root

binding.horizontalSelector.setAdapter(ExampleAdapter(weakContext.get()!!))
val adapter = ExampleAdapter(weakContext.get()!!)
binding.horizontalSelector.setAdapter(adapter)
adapter.updateArray(DataSource.arr) // No ViewModel - no bug

/**
* Method demonstrating how to get the index of the focused item.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.realwear.uxlibrary_example.horizontalselectorexample
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.realwear.uxlibrary_example.R
import com.realwear.uxlibrary_example.databinding.ActivityHorizontalSelectorBinding
Expand All @@ -19,14 +20,22 @@ import com.realwear.uxlibrary_example.databinding.ActivityHorizontalSelectorBind
*/
class HorizontalSelectorExampleActivity : AppCompatActivity() {
private lateinit var binding: ActivityHorizontalSelectorBinding

private val viewModel by viewModels<HorizontalSelectorExampleViewModel>()
private val adapter = ExampleAdapter(this)

private var centerBorderVisibility = View.VISIBLE

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHorizontalSelectorBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.horizontalSelector.setAdapter(ExampleAdapter(this))
binding.horizontalSelector.setAdapter(adapter)
viewModel.getArray().observe(this) {
// FIXME: ViewModel observing causes bug with items
adapter.updateArray(it)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.realwear.uxlibrary_example.horizontalselectorexample

import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import kotlinx.coroutines.Dispatchers

class HorizontalSelectorExampleViewModel : ViewModel() {

fun getArray() = liveData(Dispatchers.IO) {
emit(DataSource.arr)
}
}