Gestión de datos en un RecyclerView

Cómo borrar y añadir datos

Añadir y borrar items:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>

    <color name="red">#FF0D0D</color>
</resources>

item_superhero.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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:layout_marginHorizontal="16dp"
    android:layout_marginVertical="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ivSuperHero"
            android:layout_width="150dp"
            android:layout_height="150dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@drawable/ic_launcher_background" />

        <TextView
            android:id="@+id/tvSuperHeroName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="21sp"
            android:textStyle="bold"
            android:textColor="@color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ivSuperHero"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="KotlinMan" />

        <TextView
            android:id="@+id/tvRealName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ivSuperHero"
            app:layout_constraintTop_toBottomOf="@+id/tvSuperHeroName"
            tools:text="Julio" />

        <TextView
            android:id="@+id/tvPublisher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:text="Hola" />

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:backgroundTint="@color/red"
            android:text="BORRAR :("
            app:layout_constraintStart_toEndOf="@+id/ivSuperHero" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

SuperHeroViewHolder.kt

class SuperHeroViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val binding = ItemSuperheroBinding.bind(view)

    fun render(
        superHeroModel: SuperHero,
        onClickListener: (SuperHero) -> Unit,
        onClickDelete: (Int) -> Unit
    ) {
        binding.tvSuperHeroName.text = superHeroModel.superhero
        binding.tvRealName.text = superHeroModel.realName
        binding.tvPublisher.text = superHeroModel.publisher
        // Glide.with(binding.ivSuperHero.context).load(superHeroModel.photo).into(binding.ivSuperHero)
        Picasso.get().load(superHeroModel.photo).into(binding.ivSuperHero);
        itemView.setOnClickListener {
            onClickListener(superHeroModel)
        }
        binding.btnDelete.setOnClickListener { onClickDelete(adapterPosition) }
    }
}

SuperHeroAdapter.kt

class SuperHeroAdapter(
    private val superheroList: List<SuperHero>,
    private val onClickListener: (SuperHero) -> Unit,
    private val onClickDelete: (Int) -> Unit
) : RecyclerView.Adapter<SuperHeroViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SuperHeroViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return SuperHeroViewHolder(layoutInflater.inflate(R.layout.item_superhero, parent, false))
    }

    override fun onBindViewHolder(holder: SuperHeroViewHolder, position: Int) {
        val item = superheroList[position]
        holder.render(item, onClickListener, onClickDelete)
    }

    override fun getItemCount(): Int {
        return superheroList.size
    }
}

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerSuperHero"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btnAddSuperHero"
        android:layout_width="295dp"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="31dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:text="New SuperHero" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var superHeroMutableList: MutableList<SuperHero> = SuperHeroProvider.superHeroList.toMutableList()
    private lateinit var adapter: SuperHeroAdapter
    private val llmanager = LinearLayoutManager(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btnAddSuperHero.setOnClickListener { createSuperHero() }
        initRecyclerView()
    }

    private fun createSuperHero() {
        val superHero = SuperHero(
            "New SuperHero",
            "Aristides Corporation",
            "?",
            "https://pbs.twimg.com/profile_images/1037281659727634432/5x2XVPwB_400x400.jpg")

        superHeroMutableList.add(3, superHero)
        adapter.notifyItemInserted(3)
        // adapter.notifyItemInserted(superHeroMutableList.size - 1)
       llmanager.scrollToPositionWithOffset(3, 20)
    }

    private fun initRecyclerView() {
        adapter = SuperHeroAdapter(
            superheroList =  superHeroMutableList,
            onClickListener =  { superHero -> onItemSelected(superHero) },
            onClickDelete = {position -> onDeletedIem(position)}
        )
        // val manager = LinearLayoutManager(this)
        // val decoration = DividerItemDecoration(this, manager.orientation)
        val decoration = DividerItemDecoration(this, llmanager.orientation)
        binding.recyclerSuperHero.layoutManager = llmanager
        // binding.recyclerSuperHero.adapter = SuperHeroAdapter(SuperHeroProvider.superHeroList) { superHero -> onItemSelected(superHero) }
        binding.recyclerSuperHero.adapter = adapter
        binding.recyclerSuperHero.addItemDecoration(decoration)
    }

    private fun onDeletedIem(position: Int) {
        superHeroMutableList.removeAt(position)
        adapter.notifyItemRemoved(position)
    }

    private fun onItemSelected(superHero: SuperHero) {
        Toast.makeText(this, superHero.superhero, Toast.LENGTH_SHORT).show()
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(superHero.photo))
        startActivity(intent)
    }
}

 

Filtrar datos:

Código en GitHub: Filter RecyclerView

 

Deja una respuesta