Почему мой пользовательский вид не появляется внутри моего AlertDialog
, когда я пытаюсь использовать его для моего RecyclerView
адаптера?Я создал необходимые адаптеры и классы, но по какой-то причине я не вижу ни одного из своего пользовательского макета, как ожидалось.
ожидаемый результат
текущий результат
адаптер
class MyRVAdapter(private val myList: ArrayList<Facility>) : RecyclerView.Adapter<RVAdapterFacilities.ViewHolder>() {
override fun getItemCount(): Int {
return facilitiesList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Hide LinearLayout containing TextViews on fragment load
holder.mLLTextViews.visibility = View.GONE
val item = facilitiesList[position]
holder.myButton.setOnClickListener {
// Initialize a new instance of AlertDialog
val builder = android.app.AlertDialog.Builder(holder.itemView.context)
// Set the alert dialog title
builder.setTitle("Dialog title")
// Set the alert dialog icon
builder.setIcon(R.drawable.ic_info_black_24dp)
// Set the alert dialog view
builder.setView(R.layout.fragment_dialog)
// Find TabLayout and ViewPager views
val tabLayout: TabLayout = holder.itemView.findViewById(R.id.m_dialog_tabs) as TabLayout
val viewPager: ViewPager = holder.itemView.findViewById(R.id.m_dialog_viewPager) as ViewPager
// Set classes per tab, set tab names
val adapter = CustomAdapter(getChildFragmentManager())
adapter.addFragment("Boy",CustomFragment.createInstance("John"));
adapter.addFragment("Girl",CustomFragment.createInstance("Stacy"));
adapter.addFragment("Robot", CustomFragment.createInstance("Aeon"));
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
// Set TabLayout text colour
// Set TabLayout indicator colour
tabLayout.setTabTextColors(holder.itemView.resources.getColor(R.color.piccadilly, null), holder.itemView.resources.getColor(R.color.grey, null))
tabLayout.setSelectedTabIndicatorColor(holder.itemView.resources.getColor(R.color.grey, null))
// Display a positive button on alert dialog
builder.setPositiveButton(R.string.ok){ dialog, _ -> dialog.dismiss()}
// Finally, make the alert dialog using 'builder'
val dialog: android.app.AlertDialog = builder.create()
// Display the alert dialog on app interface
dialog.show()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
return ViewHolder(v)
}
// button view holder
class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
val myBtn = itemView.findViewById<Button>(R.id.my_btn)!!
}
}
Класс CustomFragment
class CustomFragment : androidx.fragment.app.Fragment() {
private var mText = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_sample, container, false)
(v.findViewById(R.id.textView_sample) as TextView).text = mText
return v
}
companion object {
fun createInstance(txt: String): CustomFragment {
val fragment = CustomFragment()
fragment.mText = txt
return fragment
}
}
}
Класс CustomAdapter
class CustomAdapter(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
}
}