PlantApp || #27

MainActivity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.neco_desarrollo.plantshandbook.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private val adapter = PlantAdapter()
private var editLauncher: ActivityResultLauncher<Intent>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
init()
editLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
if(it.resultCode == RESULT_OK){
adapter.addPlant(it.data?.getSerializableExtra("plant") as Plant)
}
}
}

private fun init() {
binding.apply {
rcView.layoutManager = GridLayoutManager(this@MainActivity, 3)
rcView.adapter = adapter
buttonAdd.setOnClickListener {
editLauncher?.launch(Intent(this@MainActivity, EditActivity::class.java))
}
}
}
}

EditActivity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.neco_desarrollo.plantshandbook.databinding.ActivityEditBinding

class EditActivity : AppCompatActivity() {
lateinit var binding: ActivityEditBinding
private var indexImage = 0
private var imageId = R.drawable.plant1
private val imageIdList = listOf(
R.drawable.plant1,
R.drawable.plant2,
R.drawable.plant3,
R.drawable.plant4,
R.drawable.plant5,
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityEditBinding.inflate(layoutInflater)
setContentView(binding.root)
initButtons()
}

private fun initButtons() = with(binding){
bNext.setOnClickListener {
indexImage++
if(indexImage > imageIdList.size - 1) indexImage = 0
imageId = imageIdList[indexImage]
imageView.setImageResource(imageId)
}
bDone.setOnClickListener {
val plant = Plant(imageId, edTitle.text.toString(), edDesc.text.toString())
val editIntent = Intent().apply {
putExtra("plant", plant)
}
setResult(RESULT_OK, editIntent)
finish()
}
}
}

Plant

import java.io.Serializable

data class Plant(val imageId: Int, val title: String, val desc: String): Serializable

PlantAdapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.neco_desarrollo.plantshandbook.databinding.PlantItemBinding
import java.util.ArrayList

class PlantAdapter: RecyclerView.Adapter<PlantAdapter.PlantHolder>() {
val plantList = ArrayList<Plant>()
class PlantHolder(item: View): RecyclerView.ViewHolder(item) {
val binding = PlantItemBinding.bind(item)
fun bind(plant: Plant) = with(binding){
im.setImageResource(plant.imageId)
tvTitle.text = plant.title
}
}

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

override fun onBindViewHolder(holder: PlantHolder, position: Int) {
holder.bind(plantList[position])
}

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

fun addPlant(plant: Plant){
plantList.add(plant)
notifyDataSetChanged()
}
}

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"
android:background="#CACACA"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcView"
android:layout_width="0dp"
android:layout_height="0dp"
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/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Add plant"
app:layout_constraintBottom_toBottomOf="@+id/rcView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_edit.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"
android:background="#BDBDBD"
tools:context=".EditActivity">

<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/cardView"
app:layout_constraintStart_toStartOf="@+id/cardView"
app:layout_constraintTop_toBottomOf="@+id/cardView"
app:layout_constraintVertical_bias="0.0">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/edTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName" />

<EditText
android:id="@+id/edDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:ems="10"
android:hint="Description"
android:inputType="textPersonName" />

<Button
android:id="@+id/bDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="done" />
</LinearLayout>
</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_margin="5dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/plant1" />
</LinearLayout>
</androidx.cardview.widget.CardView>

<Button
android:id="@+id/bNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="5dp"
android:text="next image"
app:layout_constraintBottom_toBottomOf="@+id/cardView"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

plant_item.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="wrap_content">

<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:orientation="vertical">

<ImageView
android:id="@+id/im"
android:layout_width="match_parent"
android:layout_height="100dp"
app:srcCompat="@drawable/plant1" />

<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="TextView"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *