Last active
July 6, 2021 11:12
-
-
Save iamnaran/ddd2dd01cfc5a64dd17a752a7a7e3929 to your computer and use it in GitHub Desktop.
ViewPager2 without fragment adapter
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
import android.content.Context | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import androidx.recyclerview.widget.RecyclerView | |
class PagerAdapter(private val context: Context, private val titleList: List<String>) : | |
RecyclerView.Adapter<PagerAdapter.PageHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageHolder = | |
PageHolder( | |
LayoutInflater.from(context).inflate(R.layout.item_layout_view_pager, parent, false) | |
) | |
override fun onBindViewHolder(holder: PageHolder, position: Int) { | |
holder.textView.text = titleList[position] | |
} | |
override fun getItemCount(): Int = titleList.size | |
inner class PageHolder(view: View) : RecyclerView.ViewHolder(view) { | |
val textView: TextView = view.findViewById<TextView>(R.id.title) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment