@Suppress("LocalVariableName", "PrivatePropertyName", "FunctionName")
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val ExampleItems: ArrayList<TestItems> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
binding.fab.setOnClickListener { view ->
Snackbar.make(view, "Coding Makes Me Happy", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val mainView = findViewById<RecyclerView>(R.id.MainView)
mainView.layoutManager = LinearLayoutManager(this)
mainView.adapter = TestAdapter()
LoadSampleData()
}
private fun LoadSampleData() {
ExampleItems.add(TestItems(R.drawable.ic_launcher_background, "Name1"))
ExampleItems.add(TestItems(android.R.drawable.checkbox_off_background, "Name2"))
ExampleItems.add(TestItems(android.R.drawable.bottom_bar, "Name3"))
ExampleItems.add(TestItems(android.R.drawable.btn_default_small, "Name4"))
ExampleItems.add(TestItems(android.R.drawable.btn_default_small, "Name5"))
ExampleItems.add(TestItems(R.drawable.ic_launcher_background, "Name6"))
ExampleItems.add(TestItems(R.drawable.ic_launcher_background, "Name7"))
ExampleItems.add(TestItems(R.drawable.ic_launcher_background, "Name8"))
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
this is my main activity:
package com.example.recyclerviewpractice
import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.recyclerviewpractice.databinding.ActivityMainBinding import com.google.android.material.snackbar.Snackbar
@Suppress("LocalVariableName", "PrivatePropertyName", "FunctionName") class MainActivity : AppCompatActivity() {
}
This is my Adapter:
package com.example.recyclerviewpractice
import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide
@Suppress("PropertyName") class TestAdapter: RecyclerView.Adapter() {
private val dataSet: ArrayList = ArrayList()
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
val ShowImage: ImageView = itemView.findViewById(R.id.Sample_Image)
val ShowText: TextView = itemView.findViewById(R.id.Sample_Text)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_content, parent, false)
return ViewHolder(view)
}
}
this is main Activity.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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.coordinatorlayout.widget.CoordinatorLayout>
and this is the item content:
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="10dp" app:cardCornerRadius="10dp"> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Sample_Image" android:src="@mipmap/ic_launcher" android:contentDescription="@string/this_does_something" />
</androidx.cardview.widget.CardView>