Created
October 24, 2017 04:40
-
-
Save SalaSuresh/110fe90d57b231d47433fb22efb7a391 to your computer and use it in GitHub Desktop.
Android RecyclerView example using Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.example.suresh.recyclerviewpractice.MainActivity" | |
tools:layout_editor_absoluteY="81dp"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_names" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:padding="5dp"> | |
<ImageView | |
android:id="@+id/image_picture" | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
android:src="@mipmap/ic_launcher_round" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="5dp"> | |
<TextView | |
android:id="@+id/text_name" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Name" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/text_message" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Message" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suresh.recyclerviewpractice | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.support.v7.widget.DividerItemDecoration | |
import android.support.v7.widget.LinearLayoutManager | |
import android.support.v7.widget.RecyclerView | |
import android.widget.Toast | |
class MainActivity : AppCompatActivity(), MyRecyclerView.itemClickListener { | |
val names: ArrayList<String> = ArrayList() | |
val message: ArrayList<String> = ArrayList() | |
val picture: ArrayList<Int> = ArrayList() | |
override fun onItemClick(position: Int) { | |
Toast.makeText(this, "${names.get(position)}", Toast.LENGTH_SHORT).show() | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
for (i in 1..10) { | |
names.add("name " + i) | |
message.add("message " + i) | |
picture.add(R.mipmap.ic_launcher) | |
} | |
val recyclerView: RecyclerView = findViewById(R.id.recycler_names) | |
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) | |
recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) | |
val myAdapter = MyRecyclerView(names, message, picture, this, this) | |
recyclerView.adapter = myAdapter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suresh.recyclerviewpractice | |
import android.content.Context | |
import android.os.Build | |
import android.support.annotation.RequiresApi | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.ImageView | |
import android.widget.TextView | |
/** | |
* Created by Dumadu on 23-Oct-17. | |
*/ | |
class MyRecyclerView(val names: ArrayList<String>, val message: ArrayList<String>, | |
val picture: ArrayList<Int>, val context: Context, val itemClick: itemClickListener) : RecyclerView.Adapter<MyRecyclerView.MyViewHolder>() { | |
interface itemClickListener { | |
fun onItemClick(position: Int) | |
} | |
override fun getItemCount(): Int { | |
return names.size | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { | |
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) | |
return MyViewHolder(view, context, itemClick) | |
} | |
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) | |
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { | |
holder.bindData(names, message, picture, position, context) | |
} | |
class MyViewHolder(itemView: View, val context: Context, val itemClick: itemClickListener) : RecyclerView.ViewHolder(itemView) { | |
val mTextName: TextView = itemView.findViewById(R.id.text_name) | |
val mTextMessage: TextView = itemView.findViewById(R.id.text_message) | |
val mImagePicture: ImageView = itemView.findViewById(R.id.image_picture) | |
@RequiresApi(Build.VERSION_CODES.LOLLIPOP) | |
fun bindData(names: ArrayList<String>, message: ArrayList<String>, picture: ArrayList<Int>, position: Int, context: Context) { | |
mTextName.text = names.get(position) | |
mTextMessage.text = message.get(position) | |
mImagePicture.setImageDrawable(context.getDrawable(picture.get(position))) | |
itemView.setOnClickListener(View.OnClickListener { | |
itemClick.onItemClick(adapterPosition) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment