diff --git a/app/build.gradle b/app/build.gradle index debcfaf..92a6009 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,18 +4,22 @@ plugins { } android { - compileSdk 35 + compileSdk 36 namespace = "otus.gpb.homework.viewandresources" defaultConfig { applicationId "otus.gpb.homework.viewandresources" minSdk 23 - targetSdk 35 + targetSdk 36 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } + buildFeatures { + viewBinding = true + } + buildTypes { release { minifyEnabled false diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt b/app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt index b6cbf73..c8c8962 100644 --- a/app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt +++ b/app/src/main/java/otus/gpb/homework/viewandresources/CartActivity.kt @@ -2,10 +2,43 @@ package otus.gpb.homework.viewandresources import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView class CartActivity : AppCompatActivity() { + + private val cartItems = mutableListOf() + + private val cartAdapter by lazy { CartAdapter() } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_cart) + setContentView(R.layout.activity_cart_recycler_view) + + // Генерируем тестовые данные + generateTestData() + + val rv = findViewById(R.id.recyclerView) + rv.adapter = cartAdapter + + cartAdapter.setList(cartItems) + } + + private fun generateTestData() { + cartItems.clear() + for (i in 1..100) { + cartItems.add( + CartItem( + image = R.drawable.cart_item, + title = getString(R.string.cart_item_headline, i), + category = getString(R.string.cart_item_category), + description = getString(R.string.cart_item_description), + price = i * 10 + ) + ) + } + + val cartCounter = findViewById(R.id.cartCounter) + cartCounter.text = getString(R.string.cart_items, 100) } } \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/CartAdapter.kt b/app/src/main/java/otus/gpb/homework/viewandresources/CartAdapter.kt new file mode 100644 index 0000000..b6784c8 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/viewandresources/CartAdapter.kt @@ -0,0 +1,30 @@ +package otus.gpb.homework.viewandresources + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView + +class CartAdapter() : RecyclerView.Adapter() { + + private var list = emptyList() + + fun setList(list: List) { + this.list = list.toList() + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CartViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.cart_item, parent, false) + + return CartViewHolder(view) + } + + override fun getItemCount() = list.size + + override fun onBindViewHolder(viewHolder: CartViewHolder, position: Int) { + val item = list[position] + viewHolder.bind(item) + } + +} \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt b/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt new file mode 100644 index 0000000..be47220 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/viewandresources/CartItem.kt @@ -0,0 +1,9 @@ +package otus.gpb.homework.viewandresources + +data class CartItem( + val image: Int, + val title: String, + val category: String, + val description: String, + val price: Int +) \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/CartViewHolder.kt b/app/src/main/java/otus/gpb/homework/viewandresources/CartViewHolder.kt new file mode 100644 index 0000000..bc256c3 --- /dev/null +++ b/app/src/main/java/otus/gpb/homework/viewandresources/CartViewHolder.kt @@ -0,0 +1,26 @@ +package otus.gpb.homework.viewandresources + +import android.view.View +import android.view.ViewGroup +import android.widget.ImageButton +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +class CartViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + private val title: TextView by lazy { view.findViewById(R.id.name) } + private val category: TextView by lazy { view.findViewById(R.id.category) } + private val description: TextView by lazy { view.findViewById(R.id.description) } + private val price: TextView by lazy { view.findViewById(R.id.price) } + private val photo: ImageView by lazy { view.findViewById(R.id.image) } + fun bind(item: CartItem) { + title.text = item.title + price.text = buildString { + append("$") + append(item.price.toString()) + } + category.text = item.category + description.text = item.description + photo.setImageResource(item.image) + } +} \ No newline at end of file diff --git a/app/src/main/java/otus/gpb/homework/viewandresources/MainActivity.kt b/app/src/main/java/otus/gpb/homework/viewandresources/MainActivity.kt index 22b779c..ac1dcfe 100644 --- a/app/src/main/java/otus/gpb/homework/viewandresources/MainActivity.kt +++ b/app/src/main/java/otus/gpb/homework/viewandresources/MainActivity.kt @@ -2,11 +2,19 @@ package otus.gpb.homework.viewandresources import android.content.Intent import android.os.Bundle +import android.view.View import android.widget.Button +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.android.material.dialog.MaterialAlertDialogBuilder +import androidx.appcompat.app.AlertDialog +import com.google.android.material.button.MaterialButton +import com.google.android.material.textfield.TextInputEditText class MainActivity : AppCompatActivity() { + + private val login_Dialog by lazy { createMaterialDialog() } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -19,9 +27,28 @@ class MainActivity : AppCompatActivity() { } findViewById