is it possible to get `RecyclerView` from Fragment? - android

trying to get RecyclerView from this layout :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".listFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclee">
</android.support.v7.widget.RecyclerView>
into main activity class :
private var mBlogList = findViewById<RecyclerView>(R.id.recyclee)
getting error :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
any help please :)
edit 1
i use kotlin extension now
import kotlinx.android.synthetic.main.fragment_list.*
class MainActivity : AppCompatActivity() {
private lateinit var mBlogList : RecyclerView
in onCreate method :
mBlogList = recyclee
but the same error still exist
edit 2
listFragment code :
class listFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclee != null
}
companion object {
fun newInstance(): listFragment = listFragment()
}
}
edit 3
whole MainActivity code:
//this app supposed to read from FirebaseDatabase
//into Recycler view
//the RecyclerView is into Fragment layout
//i use Fragments into FrameLayout in the activity_main.xml
// the RecyclerView should be shown when navigatoinBar is clicked
//or on start of MainActivity
class MainActivity : AppCompatActivity() {
private var mDatabase:DatabaseReference? = null
private lateinit var mBlogList : RecyclerView
private var query:Query?=null
private var options:FirebaseRecyclerOptions<Blog>?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//start listFragment , RecyclerView is there
val mFragment = listFragment.newInstance()
//openFragment method is below
openFragment(mFragment)
//navigation bottom onclicklistener
navBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
//get data from database
mDatabase=FirebaseDatabase.getInstance().getReference().child("mall")
mDatabase?.keepSynced(true)
//here i should have recyclee but it is null i don't know why
mBlogList = recyclee
mBlogList.setHasFixedSize(true)
mBlogList.layoutManager = LinearLayoutManager(this)
//query of database
query = mDatabase?.orderByKey()
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
//there are 4 icons in the navigation_bottom_bar
//now we are talking about listNav icon only because it is realted
// with listFragment
when (item.itemId) {
R.id.listNav -> {
val mFragment = listFragment.newInstance()
openFragment(mFragment)
return#OnNavigationItemSelectedListener true
}
R.id.cartNav -> {
val mFragment = cartFragment.newInstance()
openFragment(mFragment)
return#OnNavigationItemSelectedListener true
}
R.id.supportNav -> {
val mFragment = supportFragment.newInstance()
openFragment(mFragment)
return#OnNavigationItemSelectedListener true
}
R.id.accountNav -> {
val mFragment = accountFragment.newInstance()
openFragment(mFragment)
return#OnNavigationItemSelectedListener true
}
}
false
}
private fun openFragment(fragment: Fragment) {
//open Fragment into FrameLayout in the main_activity.xml
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.mainFrame, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
override fun onStart() {
super.onStart()
//set options for FirebaseRecyclerAdapter
options = FirebaseRecyclerOptions.Builder<Blog>()
.setQuery(query!!, Blog::class.java)
.build()
//set custom adapter
val mAdapter = object : FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
options!!) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BlogViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.cardview, parent, false)
return BlogViewHolder(view)}
override fun onBindViewHolder(holder: BlogViewHolder, position: Int, model: Blog) {
holder.setTitle(model.title)
holder.setDes(model.des)
holder.setImage(applicationContext, model.image)
}
}
mBlogList.adapter = mAdapter
}
inner class BlogViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var mView:View= itemView
//set title, des amd image with data we got from database
fun setTitle(title:String){
var postTitle = mView.findViewById<TextView>(R.id.post_title)
postTitle?.text = title
}
fun setDes(des:String){
var postDes = mView.findViewById<TextView>(R.id.post_des)
postDes?.text = des
}
fun setImage(image:String){
var postImage = mView.findViewById<ImageView>(R.id.post_title)
Picasso.get().load(image).into(postImage)
}
}
}

If you apply kotlin-android-extensions plugin then you don't need to use findViewById anymore because you can access views in the layout as if they're properties (by name). So, your onCreate could look as follows:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
assert recyclee != null
}
Edit
Since you're trying to do this in Fragment and your layout file is called
fragment_List.xml then in your Fragment you must inflate the layout first and then you can access your RecyclerView as in the example above for Activity:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_List, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
assert recyclee != null
}
If you try to access your RecyclerView in Activity before fragment_List is inflated in the Fragment then it will obviously be null (it hasn't been created yet). That's actually what you're trying to do in onCreate - it's too early as the Fragment's layout hasn't been built yet.
Edit 2
As it's seen from your code, mBlogList = recyclee is set in onCreate. Even though it's done after the ListFragment is created, it's still too early as its onCreateView hasn't been called yet and so no layout is in place.
A quick fix would be to do it in onStart as at that point ListFragment is definitely there. However, a better approach is to do the logic inside ListFragment itself and communicate with MainActivity via callbacks as described here

Try to use the findViewById on onCreate or onCreateView (if you are in a Fragment). It is happening because before this methods your layout wasn't inflated yet so Android cannot find your RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var mBlogList : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mBlogList = findViewById<RecyclerView>(R.id.recyclee)
}
}
I would recommend you to read this: https://kotlinlang.org/docs/tutorials/android-plugin.html
Kotlin has a better way to get the reference os your views, you don't need to use findViewById =)

You cannot call findViewById from the global scope. But, you can declare it as:
private val mBlogList by lazy { findViewById<RecyclerView>(R.id.recyclee) }
...then use the variable the usual.

In this way you could access RecyclerView attributes once adapter has been loaded at onCreate() function
var recyclerView : RecyclerViewAdapterYourClass = adapter as RecyclerViewAdapterYourClass
recyclerView.yourRecyclerAttribute

Related

How to open a fragment with button click in recycler view

class Crypto : Fragment(R.layout.fragment_crypto) {
private lateinit var recyclerView: RecyclerView
private lateinit var cryptolist: ArrayList<crypro_data>
private lateinit var cryptoAdapter: cryptoAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView = view.findViewById(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
cryptolist = ArrayList()
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lira"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"sterlingi"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lira"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"sterlingi"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))
cryptoAdapter = cryptoAdapter(cryptolist)
recyclerView.adapter = cryptoAdapter
}
}
this is my code on a fragment where i have recyclerview
i would like to know if it would be possible to open a same fragment when clicked on any of the items in the recyclerview.
but each item has to transfer its unique id to opened fragment in order to find out which button was clicked.
To open a Fragment with a button click in a RecyclerView, you can use the setOnClickListener method on the button in your RecyclerView's adapter together with a click listener in the Fragment from where you create the adapter.
Here is an example of how you can do this in an adapter:
class ImageAdapter(private var imageModel: List<ImageModel>) : RecyclerView.Adapter<ImageAdapter.PostViewHolder>() {
...
//The click listener
var onButtonClick: ((ImageModel) -> Unit)? = null
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
...
//Set the click listener on the button you have in your RV element
holder.itemView.button.setOnClickListener {
//Clicked
onButtonClick?.invoke(imageModel[position])
}
}
}
In your Fragment, you can implement the listener and set it to the adapter and handle the transition:
lass YourFragment : Fragment() {
...
private val imageAdapter = ImageAdapter(listOf())
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
imageAdapter.onButtonClick = { imageModel ->
// Handle the button click here
// open a new fragment
val fragment = AnotherFragment()
val fragmentManager = childFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
// Pass object as extra
bundle.putParcelable("key", imageModel)
fragment.arguments = bundle
fragmentTransaction.replace(R.id.fragment_container, fragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
}
}
And in the AnotherFragment, you can retrieve the object from the arguments property in the onCreate method:
class AnotherFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Retrieve object from extra
val myObject = arguments?.getParcelable<MyObject>("key")
}
}
In this example, the class ImageModel,should implement the Parcelable interface.
Happy Coding!

Passing adapter text to fragment creating problems

I want to pass data to fragment so when items in recycler adapter clicked it pass item name (sample[position].text1) fetched from firebase to fragment. I tried bundle, interface but getting error in both methods.I searched on internet but not find anything which solve my problem. mainActivity(splash screen) is only Activity in my App rest are fragments.
I used inner class method, I'm getting result but in another fragment where this adapter attached and I don't want it there.
Problem: pass sample[position].text1 to fragment so I can pass it to db.collection("here") to fetch data from Firebase.
Adapter
class dashboard_gridlayout_adapter(
private val sampledata: ArrayList<daxhboard_gridlayout_data>
): Adapter<dashboard_gridlayout_adapter.dashboard_viewholder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): dashboard_viewholder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.dashboard_gridlayout_single_item_design, parent, false)
return dashboard_viewholder(itemView)
}
override fun onBindViewHolder(holder: dashboard_viewholder, position: Int) {
Glide.with(holder.itemView).load(sampledata[position].imageResource)
.placeholder(R.drawable.ic_baseline_history_icon)
.into(holder.imageView)
holder.textView.text = sampledata[position].text1
holder.itemView.setOnClickListener {
val appCompatActivity = it.context as AppCompatActivity
appCompatActivity.supportFragmentManager.beginTransaction()
.replace(R.id.Activity_frag_container, service_providers_list())
.addToBackStack(null)
.commit()
}
}
override fun getItemCount() = sampledata.size
inner class dashboard_viewholder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.dashboard_adapter_image
val textView: TextView = itemView.dashboard_adapter_text
}
}
Fragment
class service_providers_list : Fragment(){
private var db = FirebaseFirestore.getInstance()
private lateinit var service_list_recycler: RecyclerView
var servlist = ArrayList<service_provider_list_data>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.service_providers_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getserviceproviderdata()
service_list_recycler = service_provider_recycle_view.findViewById(R.id.service_provider_recycle_view)
service_provider_recycle_view.layoutManager = LinearLayoutManager(this.requireContext())
service_provider_recycle_view.setHasFixedSize(true)
}
private fun getserviceproviderdata() {
db.collection("Barber").orderBy("dist")
.get()
.addOnSuccessListener { documents ->
servlist.clear()
for (document in documents) {
val imgurl = document.data["imageResource"].toString()
val prov_name = document.data["provider_name"].toString()
val prov_address = document.data["provider_address"].toString()
val prov_rate = document.data["provider_rating"].toString()
val prov_dist = document.data["provider_distance"].toString()
servlist.add(service_provider_list_data(imgurl, prov_name, prov_address, prov_rate, prov_dist))
service_provider_recycle_view.adapter = service_provider_list_adapter(servlist)
}
}
.addOnFailureListener { exception ->
Log.e("serf", "Error getting documents: ", exception)
}
}
}
MainActivity (It's a splash screen)
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
#Suppress("DEPRECATION")
Handler().postDelayed(
{
supportFragmentManager.beginTransaction().replace(R.id.Activity_frag_container,Login_Screen()).commit()
},
1500
)
}
}
I solved this problem
just add new parameter(need to pass) inside replace in adapter
holder.itemView.setOnClickListener {
val datashares = sampledata[position].text1
val appCompatActivity = it.context as AppCompatActivity
appCompatActivity.supportFragmentManager.beginTransaction()
.replace(R.id.Activity_frag_container, service_providers_list(datashares))
.addToBackStack(null)
.commit()
}
and inside fragment just add
class service_providers_list(datashares: String) Fragment(){
//variable declaration
private var datasharae = datashares
(inside function where i wnt to add code i.e getserviceproviderdata() )
fun getserviceproviderdata() {
db.collection(datasharae)
.............
..............
......rest code.....
.........}

How to manipulate fragment dynamically in view pager when data returned by fragment is empty

In my activity I am setting ViewPager,TabLayout and adding two fragment instance in a list. Then i am passing that list to ViewPagerAdapter .Responsibility of fragment is to fetch data from api call and show it in a list .
I am taking two instance of fragment because api returns two list of data that need to be show in tab fashion(One list in one tab and one in another). But when viewpager adapter returns fragment , if one data list is empty then I am getting empty screen in Tab-0 .
How to dynamically detect data size (here confused , because need to call fragment) and populate tab based on that.
ActivityOne.kt
class ActivityOne : BaseActivity() {
lateinit var item: ArrayList<HistoryTabItem>
lateinit var tabLayout: Tabs
val InfoViewpagerAdapter:InfoVIewPagerAdapter by lazy { InfoVIewPagerAdapter(supportFragmentManager, ArrayList()) }
fun newInstance(context: Context): Intent {
return Intent(context, InfoFragment::class.java)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.info_tabview)
getFragments()
InfoViewpagerAdapter.arrayList = item
tabLayout = findViewById(R.id.tabsnfo_type)
val viewPager = findViewById<ViewPager>(R.id.view_pager_info_type)
viewPager.adapter = InfoViewpagerAdapter
tabLayout.setupWithViewPager(viewPager)
}
fun getFragments() {
item = ArrayList()
val HistoryTabItemSeller = HistoryTabItem()
HistoryTabItemSeller.fragment = InfoFragment.createInstance()
item.add(HistoryTabItemSeller)
val HistoryTabItemBuyer = HistoryTabItem()
HistoryTabItemBuyer.fragment = InfoFragment.createInstance()
item.add(HistoryTabItemBuyer)
}
}
InfoViewPageradapter
class InfoVIewPagerAdapter(fm: FragmentManager, var arrayList: ArrayList<HistoryTabItem>) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
return arrayList[position].fragment
}
override fun getCount(): Int {
return arrayList.size
}
}
Fragment
class InfoFragment : BaseDaggerFragment(), InfoContract.View {
var isTickerShow: Boolean? = false
var tickerMessage: String? = null
lateinit var allTransactionList: ArrayList<Any>
#Inject
lateinit var infoPresenter: HoldInfoPresenter
val infoAdapter: InfoAdapter by lazy {
InfoAdapter(ArrayList()) }
lateinit var fakelist: ArrayList<Any>
companion object {
fun createInstance(): Fragment {
return InfoFragment()
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_container_info, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
infoPresenter.attachView(this)
infoPresenter.getInfo()
}
fun initView() {
rv_container.layoutManager = LinearLayoutManager(context)
rv_container.adapter = infoAdapter
}
override fun renderInfo(depositHistory: DepositHistory?) {
var resultList = ArrayList<Any>()
depositHistory?.let {
resultList = combinedTransactionList(it.sellerData as ArrayList<SellerDataItem>, it.buyerData as ArrayList<BuyerDataItem>)
isTickerShow = it.tickerMessageIsshow
tickerMessage = it.tickerMessageId
}
infoAdapter.list.clear()
infoAdapter.list.addAll(resultList)
infoAdapter.notifyDataSetChanged()
}
fun combinedTransactionList(arrayList: ArrayList<SellerDataItem>, arrayList1: ArrayList<BuyerDataItem>): ArrayList<Any> {
allTransactionList = ArrayList()
allTransactionList.clear()
allTransactionList.addAll(arrayList)
allTransactionList.addAll(arrayList1)
return allTransactionList
}
}
The best option is to fetch data in the activity and then show that tab layout with 2 tabs or just one tab. You would use it with a shared view model, but I see you don't use view models here.
You can also just set the list in createInstance() method in Fragment when it isn't empty.
The third option is to fetch data in Fragment and then send information to activity that the list is empty and hide the specific tab.

kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextView

i am trying to code my app in kotlin but i am getting null cannot be casted to non-null type and it force my app to close. i tried to reference to other stackoverflow where they shift the init but i do not have any init portion in my current code. any help will be appreciated thank you.
reminder_fragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val v = inflater.inflate(R.layout.reminder_fragment, container, false)
//getting recyclerview from xml
val recyclerView = v.findViewById(R.id.reminderrecycler) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL ,false)
//crating an arraylist to store users using the data class user
val reminderlist = ArrayList<reminders>()
//adding some dummy data to the list
reminderlist.add(reminders("Belal Khan"))
reminderlist.add(reminders("Ramiz Khan"))
reminderlist.add(reminders("Faiz Khan"))
reminderlist.add(reminders("Yashar Khan"))
//creating our adapter
val adapter = CustomAdapter(reminderlist)
//now adding the adapter to recyclerview
recyclerView.adapter = adapter
return v;
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
val manager = supportFragmentManager
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_reminders -> {
createReminderFragment()
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bottom_navigation.setItemIconTintList(null);
createReminderFragment()
bottom_navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
/*private fun replaceFragment(fragment: Fragment) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentplaceholder, fragment)
fragmentTransaction.commit();
}*/
fun createReminderFragment() {
val transaction = manager.beginTransaction()
val fragment = reminder_fragment()
transaction.replace(R.id.fragmentplaceholder, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
reminder_fragment.xml
<LinearLayout
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=".reminder_fragment"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/reminderrecycler"/>
</LinearLayout>
CustomAdapter.kt
class CustomAdapter(val reminderlist: ArrayList<reminders>) :
RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.reminder_fragment, parent, false)
return ViewHolder(v);
}
override fun getItemCount(): Int {
return reminderlist.size }
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindItems(reminderlist[position])
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(reminders: reminders) {
val medicineText = itemView.findViewById(R.id.medicineText) as TextView
medicineText.text = reminders.medicineN
}
}
}
You does not implement your RecyclerView well.
You must have an seperated layout for RecyclerView items .
You set your RecyclerView items layout , your layout which your RecylerView decalared in it now .
Therefor android can not find your TextView with Id medicineText
I recommend you to do these:
1) Create a seperate layout for your RecyclerView items and inflate it to your holder as below :
val v = LayoutInflater.from(parent.context).inflate(R.layout.recylerView_item, parent, false)
2) put in an TextView in RecylerView item layout with id of medicineText
3)Use ? symbol to handle possible NullPonterException when you declaring your TextView
val medicineText = itemView.findViewById(R.id.medicineText) as? TextView

dialog containing ViewPager

I want to create a dialog which contain's ViewPager inside it which have 3 pages and all pages have different layout structure. I want a solution by that i can set the layout content programmatically . I think this can be done by making fragments for each page but i don't know how to do this.
I go through these answers but i am not getting idea how to use them in my case.
Viewpager in Dialog?
ViewPager in Custom Dialog
ViewPager in simple Dialog
You can try and build your custom dialog through DialogFragment. Consider the XML layout would contain a ViewPager and the code to go about would be:
class PagerDialog : DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.element_fragment_pager_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupPager()
}
private fun setupPager() {
val pagerFragment1 = PagerFragment1.newInstance()
val pagerFragment2 = PagerFragment2.newInstance()
val pagerFragment3 = PagerFragment3.newInstance()
viewPager?.adapter = MyFragmentPagerAdapter(childFragmentManager).apply {
adapterReference = object : PageAdapterInterface {
override var fragmentList: List<Fragment> =
mutableListOf(pagerFragment1, pagerFragment2, pagerFragment3)
}
}
}
companion object {
const val tag = "PagerDialog"
}
}
I have used reference to the list because it might cause leaks when not handled correctly. So the PagerAdapterInterface would look like:
interface PageAdapterInterface {
var fragmentList: List<Fragment>
fun getItemCount() = fragmentList.size
#Throws(StackOverflowError::class)
fun getItemAt(index: Int) : Fragment {
if (index >= fragmentList.size) throw StackOverflowError()
return fragmentList[index]
}
}
Your view pager adapter can make use of this reference in manner that is accessing referentially like:
class MyFragmentPagerAdapter(childFragmentManager: FragmentManager) : FragmentStatePagerAdapter(childFragmentManager){
lateinit var adapterReference: PageAdapterInterface
override fun getItem(p0: Int): Fragment = adapterReference.getItemAt(p0)
override fun getCount(): Int = adapterReference.getItemCount()
}
Finally in your Activity or Fragment on create() or onViewCreated() functions respectively, you can initialize the dialog as shown:
class MyActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// use childFragmentManager if the code is
// used within the Fragment
val prev = supportFragmentManager.findFragmentByTag(PagerDialog.tag)
if (prev != null) {
supportFragmentManager.beginTransaction()
.remove(prev)
.addToBackStack(null)
.commit()
}
PagerDialog().show(supportFragmentManager, PagerDialog.tag)
}
}
Note: DialogFragment is deprecated on > API 28 check out https://developer.android.com/reference/android/app/DialogFragment

Categories

Resources