Справочник рыбака на Kotlin: Урок 9

MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.navigation.NavigationView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.main_content.*
import java.util.ArrayList


class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nav_view.setNavigationItemSelectedListener (this)

var list = ArrayList<ListItem>()

list.add(ListItem(R.drawable.som,"Сом","чварчрпчаптчапт чапрапр ыарпчапрча"))
list.add(ListItem(R.drawable.nalim,"Налим","чварчрпчаптчапт чапрапр ыарпчапрча"))
list.add(ListItem(R.drawable.shuca,"Щука","чварчрпчаптчапт чапрапр ыарпчапрча"))
list.add(ListItem(R.drawable.caras,"Карась","чварчрпчаптчапт чапрапр ыарпчапрча"))
rcView.hasFixedSize()
rcView.layoutManager = LinearLayoutManager(this)
rcView.adapter = MyAdapter(list,this)


}

override fun onNavigationItemSelected(item: MenuItem): Boolean {

when(item.itemId){
R.id.id_fish -> Toast.makeText(this,"Id fish",Toast.LENGTH_SHORT).show()
R.id.id_na -> Toast.makeText(this,"Id na",Toast.LENGTH_SHORT).show()
R.id.id_sna -> Toast.makeText(this,"Id sna",Toast.LENGTH_SHORT).show()
R.id.id_history -> Toast.makeText(this,"Id history",Toast.LENGTH_SHORT).show()
}

return true
}


}

ListItem.kt

data class ListItem (
var image_id:Int,
var titleText:String,
var contentText:String
)

MyAdapter.kt

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class MyAdapter (listArray:ArrayList<ListItem>, context: Context): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
var listArrarR = listArray
var contextR = context


class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
val tvTitle = view.findViewById<TextView>(R.id.tvTitle)
val tvContent = view.findViewById<TextView>(R.id.tvContent)
val im = view.findViewById<ImageView>(R.id.im)

fun bind( listItem:ListItem, context: Context)
{
tvTitle.text = listItem.titleText
tvContent.text = listItem.contentText
im.setImageResource(listItem.image_id)
itemView.setOnClickListener(){
Toast.makeText(context,"Pressed : ${tvTitle.text}",Toast.LENGTH_SHORT).show()
}
}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(contextR)
return ViewHolder(inflater.inflate(R.layout.item_layout,parent,false))
}

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var listItem = listArrarR.get(position)
holder.bind(listItem,contextR)
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"

>
<include layout="@layout/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:menu="@menu/drawer_main_menu"
app:headerLayout="@layout/header_main_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>

main_content.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">

<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_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

item_layout.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"
android:layout_margin="5dp"
android:background="@android:drawable/editbox_background">

<ImageView
android:id="@+id/im"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/shuca" />

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="@color/design_default_color_on_secondary"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/im" />

<TextView
android:id="@+id/tvContent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="16dp"
android:text="TextView"
android:textColor="@color/cardview_dark_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTitle"
app:layout_constraintVertical_bias="0.002" />
</androidx.constraintlayout.widget.ConstraintLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

</resources>

strings.xml

<resources>
<string name="app_name">KotlinFishermenHandbook</string>
<string name="fish">Рыбы</string>
<string name="na">Наживки</string>
<string name="sna">Снасти</string>
<string name="history">Истории</string>
</resources>

dimen.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="header_main_height">170dp</dimen>
</resources>

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

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