Skip to content

Instantly share code, notes, and snippets.

View guuilp's full-sized avatar
😃
Hi!

Guilherme guuilp

😃
Hi!
View GitHub Profile
class PeopleViewModel : ViewModel() {
private val selectedPerson = MutableLiveData<Person>()
fun setSelectedPerson(person: Person){
selectedPerson.value = person
}
fun getSelectedPerson() = selectedPerson
}
class PeopleActivity : AppCompatActivity() {
private val people = listOf(
Person(1, "Person 1", "Brazil"),
Person(2, "Person 2", "USA"),
Person(3, "Person 3", "Canada"),
Person(4, "Person 4", "Russia"),
Person(5, "Person 5", "Germany")
)
class PeopleAdapter(private val people: List<Person>, private val listener: Listener) :
RecyclerView.Adapter<PeopleAdapter.ViewHolder>() {
private var selectedPerson: Person? = null
interface Listener {
fun onItemClicked(person: Person)
}
fun updateSelectedPerson(person: Person) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.apply {
checkBox.isChecked = position == mCheckedPosition
checkBox.setOnClickListener {
if(position == mCheckedPosition) {
checkBox.isChecked = false
mCheckedPosition = -1
} else {
mCheckedPosition = position
notifyDataSetChanged()
@guuilp
guuilp / group_retweet.xml
Created June 28, 2018 02:22
Constraint Layout DOJO - Solution (STEP 3)
<android.support.constraint.Group
android:id="@+id/group_retweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="retweet_top, retweet_user"/>
@guuilp
guuilp / comment.xml
Created June 28, 2018 02:18
Constraint Layout DOJO - Solution (STEP 2)
<ImageView
android:id="@+id/comment"
android:layout_width="21dp"
android:layout_height="21dp"
android:layout_marginTop="12dp"
android:src="@drawable/ic_comment"
android:tint="@color/gray"
app:layout_constraintTop_toBottomOf="@+id/text"
@guuilp
guuilp / profile_image.xml
Created June 28, 2018 02:16
Constraint Layout DOJO - Solution (STEP 1)
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:src="@drawable/profile_image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
@guuilp
guuilp / group_retweet.xml
Last active June 28, 2018 02:08
Constraint Layout DOJO - Views without constraints (STEP 3)
<android.support.constraint.Group
android:id="@+id/group_retweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="retweet_top, retweet_user"/>
@guuilp
guuilp / profile_image.xml
Created June 28, 2018 02:08
Constraint Layout DOJO - Views without constraints (STEP 1)
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:src="@drawable/profile_image"/>
@guuilp
guuilp / comment.xml
Created June 28, 2018 02:08
Constraint Layout DOJO - Views without constraints (STEP 2)
<ImageView
android:id="@+id/comment"
android:layout_width="21dp"
android:layout_height="21dp"
android:layout_marginTop="12dp"
android:src="@drawable/ic_comment"
android:tint="@color/gray"/>