hi, i have a little problem, i try to use a viewPa...
# android
t
hi, i have a little problem, i try to use a viewPager with a tabLaout to display an infinite list of data grouped by date so that the title each tab is a date (a month). I know how to use the FragmentPagerAdapter to display a limited number of fragments, but for an infinite list, I learned that we must use the FragmentStatePagerAdapter but I do not know how to use it. Can someone help me please?
d
Hmm, isn't it a drop-in replacement? At least source-code wise.
Also, check out the link in the channel description to get better help.
t
what link?
d
t
thank you
z
FragmentPagerAdapter also works fine for infinite items afaik, because it detaches the fragment meaning the views are not kept beyond the off-screen page limit 🤔 i should probably look into the big difference in FragmentStatePagerAdapter so i'm not wrong tho
so FragmentStatePagerAdapter juggles a bunch of fragments and only retains their SavedState but not the Fragment itself, interesting
t
ok how to use it? do you have a little example or link?
p
for this adapter you can refer this file, as we are building to store data of artist name and genre.
return ArtistViewHolder(view) } } }
package com.infrontofthenet.firemusic import android.content.Intent import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.firebase.ui.firestore.FirestoreRecyclerAdapter import com.firebase.ui.firestore.FirestoreRecyclerOptions import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.Query import kotlinx.android.synthetic.main.activity_artist_list.* class ArtistList : AppCompatActivity() { // connect to firestore var db = FirebaseFirestore.getInstance() // classes to store and pass query data private var adapter: ArtistAdapter? = null // Kotlin equivalent of Java ArrayList class. We decided we don't need this after all //private var artistList = mutableListOf<Artist>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_artist_list) // recycler will have linear layout recyclerViewArtists.setLayoutManager(LinearLayoutManager(this)) // query val query = db.collection("artists").orderBy("artistName", Query.Direction.ASCENDING) // pass the query results to the recycler adapter for display val options = FirestoreRecyclerOptions.Builder<Artist>().setQuery(query, Artist::class.java).build() adapter = ArtistAdapter(options) // bind the adapter to the recyclerview (adapter means the datasource) recyclerViewArtists.adapter = adapter } // start listening for changes if the the activity starts / restarts override fun onStart() { super.onStart() adapter!!.startListening() } // stop listening for data changes if the activity gets stopped override fun onStop() { super.onStop() adapter!!.stopListening() } // inner classes needed to read and bind the data private inner class ArtistViewHolder internal constructor(private val view: View) : RecyclerView.ViewHolder(view) { // not needed if we bind to the item template directly on the onBindViewHolder method below // internal fun displayArtist(artistName: String, artistGenre: String) { // // populate the corresponding textViews in the layout template inflated when adapter created below // val textViewName = view.findViewById<TextView>(R.id.textViewName) // val textViewGenre = view.findViewById<TextView>(R.id.textViewGenre) // // textViewName.text = artistName // textViewGenre.text = artistGenre // } } private inner class ArtistAdapter internal constructor(options: FirestoreRecyclerOptions<Artist>) : FirestoreRecyclerAdapter<Artist, ArtistViewHolder>(options) { override fun onBindViewHolder(p0: ArtistViewHolder, p1: Int, p2: Artist) { // pass current Artist values to the display function above //p0.displayArtist(p2.artistName.toString(), p2.artistGenre.toString()) - replaced by 2 lines below p0.itemView.findViewById<TextView>(R.id.textViewName).text = p2.artistName p0.itemView.findViewById<TextView>(R.id.textViewGenre).text = p2.artistGenre p0.itemView.setOnClickListener { val url = p2.url.toString() val i = Intent(Intent.ACTION_VIEW) i.data = Uri.parse(url) startActivity(i) } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArtistViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_artist, parent, false)