Created
June 11, 2023 18:59
-
-
Save ikhlaqmalik13/558d47e4020e376851a5bd03c235cb86 to your computer and use it in GitHub Desktop.
RecyclerView
This file contains hidden or 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"?> | |
<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" | |
tools:context=".learning.recyclerview_learning.FaheemRVActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rv_news_posts" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or 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.maple.kashin.learning.recyclerview_learning.adapters | |
import android.content.Context | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.RecyclerView | |
import com.bumptech.glide.Glide | |
import com.maple.kashin.databinding.RowFaheemNewsPostItemBinding | |
import com.maple.kashin.learning.recyclerview_learning.models.NewsPost | |
class FaheemRecyclerViewLearningAdapter( | |
private var newPosts: ArrayList<NewsPost>, | |
private var context: Context | |
) : RecyclerView.Adapter<FaheemRecyclerViewLearningAdapter.NewsItemViewHolder>() { | |
inner class NewsItemViewHolder(private val binding: RowFaheemNewsPostItemBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(newPost: NewsPost) { | |
Glide.with(context) | |
.load(newPost.imageUrl) | |
.into(binding.ivNewsImage) | |
binding.tvTitle.text = newPost.title | |
binding.tvAuthor.text = newPost.author | |
binding.tvDate.text = newPost.date | |
} | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsItemViewHolder { | |
val faheemNewsPostItemBinding: RowFaheemNewsPostItemBinding = | |
RowFaheemNewsPostItemBinding.inflate( | |
LayoutInflater.from(context), | |
parent, | |
false | |
) | |
return NewsItemViewHolder(faheemNewsPostItemBinding) | |
} | |
override fun onBindViewHolder(holder: NewsItemViewHolder, position: Int) { | |
val onePostFromNewPosts : NewsPost = newPosts[position] | |
holder.bind(onePostFromNewPosts) | |
} | |
override fun getItemCount(): Int { | |
return newPosts.size | |
} | |
} |
This file contains hidden or 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.maple.kashin.learning.recyclerview_learning | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.maple.kashin.databinding.ActivityFaheemRvactivityBinding | |
import com.maple.kashin.learning.recyclerview_learning.adapters.FaheemRecyclerViewLearningAdapter | |
import com.maple.kashin.learning.recyclerview_learning.models.NewsPost | |
class FaheemRVActivity : AppCompatActivity() { | |
private lateinit var binding: ActivityFaheemRvactivityBinding | |
private var newsPosts: ArrayList<NewsPost> = arrayListOf<NewsPost>() | |
private lateinit var rvAdapter: FaheemRecyclerViewLearningAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityFaheemRvactivityBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
addNewsPostsData() | |
setUpRecyclerView() | |
} | |
private fun setUpRecyclerView() { | |
rvAdapter = FaheemRecyclerViewLearningAdapter(newsPosts, this) | |
binding.rvNewsPosts.adapter = rvAdapter | |
binding.rvNewsPosts.layoutManager = | |
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) | |
} | |
private fun addNewsPostsData() { | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg", | |
"This is the title of first post", | |
"Ikhalq", | |
"2 hrs ago" | |
) | |
) | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/564x/44/3e/cb/443ecb7f724bc99256cec196a881cce1.jpg", | |
"This is the title of 0 post", | |
"Farhat", | |
"2 hrs ago" | |
) | |
) | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/736x/1c/3b/41/1c3b41e00fb3efd6df810739af9e4b65.jpg", | |
"This is the title of 2 post", | |
"Ikhalq", | |
"1 hrs ago" | |
) | |
) | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg", | |
"This is the title of 3 post", | |
"Ikhalq", | |
"3 hrs ago" | |
) | |
) | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpgg", | |
"This is the title of 4 post", | |
"Ikhalq", | |
"4 hrs ago" | |
) | |
) | |
newsPosts.add( | |
NewsPost( | |
"https://i.pinimg.com/564x/51/7c/af/517caf0d930f526896eb811b3869c31c.jpg", | |
"This is the title of 5 post", | |
"Faheem", | |
"5 hrs ago" | |
) | |
) | |
} | |
} |
This file contains hidden or 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.maple.kashin.learning.recyclerview_learning.models | |
data class NewsPost( | |
val imageUrl: String, | |
val title: String, | |
val author: String, | |
val date: String, | |
) |
This file contains hidden or 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"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:padding="@dimen/_6dp" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:scaleType="centerCrop" | |
android:id="@+id/iv_news_image" | |
android:layout_width="@dimen/_90dp" | |
android:layout_height="@dimen/_90dp" | |
android:src="@drawable/founder_ikhlaq" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<LinearLayout | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:padding="@dimen/_8dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/iv_news_image" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.0"> | |
<TextView | |
android:id="@+id/tv_title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Title of the post" | |
android:textColor="@color/black" | |
android:textSize="@dimen/_16sp" | |
android:textStyle="bold" /> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/tv_author" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="@dimen/_4dp" | |
android:text="Author" /> | |
<TextView | |
android:id="@+id/tv_date" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="@dimen/_4dp" | |
android:text="date" /> | |
</LinearLayout> | |
</LinearLayout> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment