Я пытаюсь связать мой RecyclerView
адаптер с фрагментом, но он ожидает другого параметра на этапе создания адаптера.Я понимаю, что в параметр необходимо включить что-то, имеющее отношение к fm
, но я не уверен, что мне нужно для этого.
Не передано значение для параметра 'fm'
Класс адаптера RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.companyname.appname.*
import com.google.android.flexbox.FlexboxLayout
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_dialog.view.*
class MyRVAdapter(private val myList: ArrayList<Item>, private val fm: FragmentManager) : RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return myList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.myBtn.text = (myList[position].textBtnTitle)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
return ViewHolder(v)
}
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val myBtn = itemView.findViewById<Button>(R.id.btn_a)!!
init {
myBtn.setOnClickListener {
val builder = AlertDialog.Builder(myBtn.context)
builder.setTitle(myList[position].txtDialogTitle)
builder.setMessage(myList[position].txtDialogMessage)
builder.setPositiveButton(android.R.string.ok){ dialog, _ -> dialog.dismiss() }
val dialog: AlertDialog = builder.create()
dialog.show()
tabLayout = customView.findViewById(R.id.mTabLayout)
viewPager = customView.findViewById(R.id.mViewPager)
val adapter = TabbedDialogAdapter(fm)
adapter.addFragment("Tab A", TabbedDialogFragment.createInstance("Description A"))
adapter.addFragment("Tab B", TabbedDialogFragment.createInstance("Description B"))
adapter.addFragment("Tab C", TabbedDialogFragment.createInstance("Description C"))
customView.mViewPager.adapter = adapter
customView.mTabLayout.setupWithViewPager(customView.mViewPager)
}
}
}
}
TabbedDialogAdapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
class TabbedDialogAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
private var mFragmentCollection: MutableList<Fragment> = ArrayList()
private var mTitleCollection: MutableList<String> = ArrayList()
fun addFragment(title: String, fragment: Fragment) {
mTitleCollection.add(title)
mFragmentCollection.add(fragment)
}
override fun getPageTitle(position: Int): CharSequence? {
return mTitleCollection[position]
}
override fun getItem(position: Int): Fragment {
return mFragmentCollection[position]
}
override fun getCount(): Int {
return mFragmentCollection.size
}
}
класс фрагмента
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.companyname.appname.R
import com.companyname.appname.MyRVAdapter
import java.util.*
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)
mRecyclerView.layoutManager = LinearLayoutManager(activity)
val myList = ArrayList<Item>()
val mAdapter = MyRVAdapter(myList)
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}