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
class UserAdapter : RecyclerView.Adapter<UserAdapter.UserHolder>(), BindableAdapter<List<Long>> { | |
override fun setData(items: List<Long>) { | |
userIds = items | |
notifyDataSetChanged() | |
} | |
var userIds = emptyList<Long>() | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder { |
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
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layoutManager="android.support.v7.widget.LinearLayoutManager" | |
app:data="@{viewModel.userIds}"/> |
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
@BindingAdapter("data") | |
fun <T> setRecyclerViewProperties(recyclerView: RecyclerView, data: T) { | |
if (recyclerView.adapter is BindableAdapter<*>) { | |
(recyclerView.adapter as BindableAdapter<T>).setData(data) | |
} | |
} |
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
interface BindableAdapter<T> { | |
fun setData(data: T) | |
} |
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
@BindingAdapter("data") | |
fun <T> setRecyclerViewProperties(recyclerView: RecyclerView, items: List<Long>) { | |
if (recyclerView.adapter is UserAdapter) { | |
(recyclerView.adapter as UserAdapter).setData(items) | |
} | |
} |
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
class UserAdapter : RecyclerView.Adapter<UserAdapter.UserHolder>() { | |
fun setData(items: List<Long>) { | |
userIds = items | |
notifyDataSetChanged() | |
} | |
var userIds = emptyList<Long>() | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder { |
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
class MainActivity : AppCompatActivity() { | |
private val viewModel = UserViewModel() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val adapter = UserAdapter() |
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
class UserViewModel : BaseObservable() { | |
@get:Bindable | |
var userIds: List<Long> = emptyList() | |
private set(value) { | |
field = value | |
notifyPropertyChanged(BR.userIds) | |
} | |
private val updateInterval = 1000L |
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
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:data="@{viewModel.userIds}"/> |
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
@Test | |
fun testOpenDetailsOnItemClick() { | |
mockRepoUsers(1) | |
activityRule.launchActivity(Intent()) | |
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)) | |
.perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, ViewActions.click())) | |
val expectedText = "User 1: 100 pts" |