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

MainActivity.kt

import android.content.res.TypedArray
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.*



class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
var adapter: MyAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nav_view.setNavigationItemSelectedListener (this)

var list = ArrayList<ListItem>()

list.addAll(fillArras(resources.getStringArray(R.array.fish),
resources.getStringArray(R.array.fish_content),getImageId(R.array.fish_image_array)))
rcView.hasFixedSize()
rcView.layoutManager = LinearLayoutManager(this)
adapter = MyAdapter(list,this)
rcView.adapter = adapter


}

override fun onNavigationItemSelected(item: MenuItem): Boolean {

when(item.itemId){

R.id.id_fish ->
{
Toast.makeText(this,"Id fish",Toast.LENGTH_SHORT).show()
adapter?.updateAdapter(fillArras(resources.getStringArray(R.array.fish),
resources.getStringArray(R.array.fish_content),getImageId(R.array.fish_image_array)))

}
R.id.id_na ->
{
Toast.makeText(this,"Id na",Toast.LENGTH_SHORT).show()
adapter?.updateAdapter(fillArras(resources.getStringArray(R.array.na),
resources.getStringArray(R.array.na_content),getImageId(R.array.na_image_array)))

}
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
}
fun fillArras(titleArray:Array<String>,contentArray:Array<String>,imageArray:IntArray):List<ListItem>
{
var listItemArray = ArrayList<ListItem>()
for(n in 0..titleArray.size - 1)
{
var listItem = ListItem(imageArray[n],titleArray[n],contentArray[n])
listItemArray.add(listItem)
}
return listItemArray
}

fun getImageId(imageArrayId:Int):IntArray
{
var tArray:TypedArray = resources.obtainTypedArray(imageArrayId)
val count = tArray.length()
val ids = IntArray(count)
for(i in ids.indices)
{
ids[i] = tArray.getResourceId(i,0)
}
tArray.recycle()
return ids
}

}

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)
}

fun updateAdapter(listArray : List<ListItem>)
{
listArrarR.clear()
listArrarR.addAll(listArray)
notifyDataSetChanged()
}
}


array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="fish">
<item>Сом</item>
<item>Налим</item>
<item>Щука</item>
<item>Карась</item>
</string-array>
<string-array name="fish_content">
<item>ырвыпаывпрыврпрырпыекр</item>
<item>ыкерыкерыкернывкернывкерыкервкервкерык</item>
<item>ыкерыкерыкрыкгеуновеновенове</item>
<item>веновкеоновеновеноеновено</item>
</string-array>
<array name="fish_image_array">
<item>@drawable/som</item>
<item>@drawable/nalim</item>
<item>@drawable/shuca</item>
<item>@drawable/caras</item>
</array>
<string-array name="na">
<item>м</item>
<item>им</item>
<item>ука</item>
<item>арась</item>
</string-array>
<string-array name="na_content">
<item>ы</item>
<item>ыкерык</item>
<item>ы</item>
<item>ве</item>
</string-array>
<array name="na_image_array">
<item>@drawable/nalim</item>
<item>@drawable/som</item>
<item>@drawable/caras</item>
<item>@drawable/shuca</item>

</array>

</resources>

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

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