Created
February 2, 2019 05:43
-
-
Save javadude/dec1862b075338790f023b42e01fd953 to your computer and use it in GitHub Desktop.
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/recycler" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginBottom="8dp" /> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/linearLayout" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/name" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
android:layout_marginEnd="8dp" | |
tools:text="Name" | |
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" | |
app:layout_constraintEnd_toStartOf="@+id/age" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/age" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:layout_marginEnd="8dp" | |
tools:text="Age" | |
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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
package com.javadude.recyclerexample1 | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.ItemTouchHelper | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlinx.android.extensions.LayoutContainer | |
import kotlinx.android.synthetic.main.activity_main.recycler | |
import kotlinx.android.synthetic.main.item.age | |
import kotlinx.android.synthetic.main.item.name | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val people = mutableListOf( | |
Person("Alex", 25), | |
Person("Claire", 21), | |
Person("Nicole", 30), | |
Person("Scott", 52), | |
Person("Trevor", 23) | |
) | |
val adapter = Adapter1(people) | |
recycler.adapter = adapter | |
recycler.layoutManager = LinearLayoutManager(this) | |
ItemTouchHelper(Swiper(adapter)).attachToRecyclerView(recycler) | |
} | |
class Swiper(val adapter: Adapter1) : ItemTouchHelper.Callback() { | |
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) = | |
makeMovementFlags(0, ItemTouchHelper.START or ItemTouchHelper.END) | |
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder) = false | |
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | |
adapter.delete(viewHolder.adapterPosition) | |
} | |
} | |
data class Person(val name : String, val age : Int) | |
class Adapter1(private val items : MutableList<Person>) : RecyclerView.Adapter<Holder1>() { | |
override fun getItemCount() = items.size | |
// init { | |
// setHasStableIds(true) | |
// } | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = | |
Holder1(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)) | |
override fun onBindViewHolder(holder: Holder1, position: Int) { | |
holder.bind(items[position]) | |
} | |
// override fun getItemId(position: Int) = position.toLong() | |
fun delete(row : Int) { | |
items.removeAt(row) | |
notifyItemRemoved(row) | |
} | |
} | |
class Holder1(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer { | |
fun bind(person : Person) { | |
name.text = person.name | |
age.text = person.age.toString() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment