No matter what I do the pictures are not showing up.
Even if I set the items to match parent, I still have the same problem, I’ve been dealing with it since the morning, if I find it, I will relax very well 😀
This is my adapter for viewpager 2 :
class ViewPagerAdapter ( private val context : Context , private val album_id : Int , private val photo_list : ArrayList<Photos> ) : RecyclerView.Adapter<ViewPagerAdapter.PagerHolder>() {
inner class PagerHolder (view : View ) : RecyclerView.ViewHolder ( view ) {
var image_view : ImageView
init {
image_view = view.findViewById ( R.id.pager_imageview )
}
}
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : PagerHolder {
val v = LayoutInflater.from ( parent.context ).inflate ( R.layout.viewpager_item , parent , false )
return PagerHolder ( v )
}
override fun onBindViewHolder (holder : ViewPagerAdapter.PagerHolder, position : Int ) {
val list = photo_list.filter { it.albumId == album_id }
for ( i in list [ position ].url ) {
Glide.with ( context ).load ( i.toString() ).diskCacheStrategy ( DiskCacheStrategy.ALL ).into ( holder.image_view )
}
}
override fun getItemCount ( ) : Int {
return photo_list.size
}
}
This is my xml code for my viewpager items :
<androidx.cardview.widget.CardView 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"
app:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/pager_imageview"
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>
</androidx.cardview.widget.CardView>
This is my recyclerview defined codes:
class AlbumRecyclerAdapter ( private val context : Context , private val album_list : ArrayList<Albums> , private val photo_list : ArrayList<Photos> ) : RecyclerView.Adapter<AlbumRecyclerAdapter.Adapterholder>() {
inner class Adapterholder(view : View) : RecyclerView.ViewHolder(view) {
var album_title : TextView
var view_pager2 : ViewPager2
init {
album_title = view.findViewById(R.id.album_title)
view_pager2 = view.findViewById ( R.id.view_pager2 )
}
}
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : Adapterholder {
val v = LayoutInflater.from ( parent.context ).inflate ( R.layout.album_recycler_list , parent , false )
return Adapterholder(v)
}
override fun onBindViewHolder ( holder : AlbumRecyclerAdapter.Adapterholder , position : Int) {
val pager_adapter = ViewPagerAdapter ( context , album_list [ position ].id , photo_list )
holder.view_pager2.orientation = ViewPager2.ORIENTATION_HORIZONTAL
holder.view_pager2.adapter = pager_adapter
holder.view_pager2.clipToPadding = false
holder.view_pager2.clipChildren = false
holder.view_pager2.offscreenPageLimit = 3
holder.view_pager2.getChildAt ( 0 ).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
holder.album_title.text = album_list [ position ].title
}
override fun getItemCount ( ) : Int {
return album_list.size
}
}
Finally , this is my xml code for the recyclerview :
<androidx.cardview.widget.CardView 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="300dp"
android:layout_marginTop ="10dp"
android:layout_marginLeft ="5dp"
android:layout_marginRight="5dp"
app:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/album_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Album Title"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view_pager2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
2
Answers
I’m Java Developer so I cannot code with Kotlin but I’ll show you how to show images on
ViewPager
.First, remove
for ( i in list [ position ].url )
fromonBindViewHolder
. And load direct fromphoto_list.get(position).getImage
as belowMake sure to change
JAVA
toKotlin
in my code. If you any problem is causing, Let me knowInstead of passing the whole
photo_list
and thenfilter
ing it inViewPager
, pass only thefilter
ed list to it. Because ingetItemCount
you are returning the size of the wholephoto_list
but inonBindViewHolder
you’re accessing thefilter
ed list, this could lead to crashes asfilter
ed list would be smaller than the original one.In
AlbumRecyclerAdapter
‘sonBindViewHolder
pass the filtered list toViewPager
adapter.In
ViewPagerAdapter
‘sonBindViewHolder()
, why are you iterating over aurl
and I assume yoururl
is aString
and when you iterate over it, it loops through all the individual characters of the string, that is why you are not seeing any image in the first place.