I have a viewpager2 with a tablayout with two tabs. In the fragment of each tab a have a button, but the onclick listener never is called when I click on it. I don't know where is the problem...
Fragment where I instance the viewpager2:
...
@BindView(R.id.tab_viewpager2_bookings)
lateinit var tabLayout: TabLayout
@BindView(R.id.viewpager2_booking)
lateinit var viewPager: ViewPager2
...
val tabs = arrayOf(getString(R.string.bookings_on_going), getString(R.string.bookings_upcoming))
tabLayout.tabGravity = TabLayout.GRAVITY_FILL
val adapter: FragmentStateAdapter = BookingsPagerAdapter(requireActivity(), tabs, ticketsOnGoing, ticketUpcoming, _user!!, bearerToken)
viewPager.adapter = adapter
if (ticketsOnGoing.isEmpty() && ticketUpcoming.isNotEmpty()) {
viewPager.currentItem = 1
}
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = tabs[position]
}.attach()
The XML with the viewpager2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_viewpager2_bookings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp"
android:visibility="visible"
app:tabIndicatorColor="@color/tabs_indicator_boardingpass"
app:tabTextColor="@color/tabs_indicator_boardingpass"></com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager2_booking"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_viewpager2_bookings" />
...
The adapter:
class BookingsPagerAdapter(fa: FragmentActivity, private val tabs:Array<String>, private val
ticketsOnGoing: List<TicketModel>, private val ticketUpcoming: List<TicketModel>,
private val user: UserModel, private val bearerToken: String) : FragmentStateAdapter(fa) {
override fun getItemCount(): Int {
return tabs.size
}
override fun createFragment(position: Int): Fragment {
when(position){
0 -> return BookingsTabFragment(ticketsOnGoing, position, user, bearerToken)
1 -> return BookingsTabFragment(ticketUpcoming, position, user, bearerToken)
}
return BookingsTabFragment(ticketsOnGoing,0, user, bearerToken)
}
}
The tab fragment:
class BookingsTabFragment(private val bookingsList: List<TicketModel>, val position: Int, val user: UserModel, private val bearerToken: String) : BaseFragment() {
lateinit var buttonTest : Button
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v: View = inflater.inflate(R.layout.fragment_tab_bookings, container, false)
if (bookingList.isEmpty()) {
llMessageBookingsFragment.visibility = View.GONE
rv_bookings_list.visibility = View.VISIBLE
buttonTest = v.findViewById<Button>(R.id.button_test)
buttonTest.setOnClickListener(View.OnClickListener {
Toast.makeText(context, "Prueba", Toast.LENGTH_SHORT).show()
//IT NEVER ENTER HERE!!
})
} else {
llMessageBookingsFragment.visibility = View.VISIBLE
rv_bookings_list.visibility = View.GONE
...
The XML of tab fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="@+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="@layout/row_bookings"
tools:visibility="visible"/>
Thanks!
I found the problem. In the tab fragment, I use RelativeLayout. The order of the components is very important for this issue, so I put the recycler view before the layout with the button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_bookings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:clipToPadding="false"
tools:listitem="@layout/row_bookings"
tools:visibility="visible"/>
<LinearLayout
android:id="@+id/ll_message_bookings_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="vertical"
tools:visibility="gone"
android:clickable="true">
<Button
android:id="@+id/button_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prueba"/>
...