title: Why I can't change or swipe tab on my TabLayout in Android?
body: <p><a href="https://i.stack.imgur.com/GeHal.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GeHal.png" alt="enter image description here"></a></p>
  
  <p>I want to make a screen like the picture above. so if I select a tab in the TabLayout, then it will change some variable before making request to server and eventually the data from server will populate the recycler view.</p>
  
  <p>I use <code>addOnTabSelectedListener</code> on TabLayout to retrieve the change If the user change the tab. usually in some tutorials, they always use tab layout with some fragment change, so it seems they use viewPager to handle it. but in this case I remove it and I just use <code>addOnTabSelectedListener</code> to get the change, and because I just need one recycler view instead of more than one fragments</p>
  
  <p>here is the code I use in my fragment</p>
  
  <pre><code>class SearchResultFragment : Fragment() {
  
      lateinit var fragmentView : View
      lateinit var recyclerView : RecyclerView
      lateinit var tabLayout : TabLayout
  
      lateinit var mContext : Context
      lateinit var mActivity : FragmentActivity
  
      private var selectedEventType = EventType.kajianUmum
      private var selectedCity = City.defaultCityName
      private var selectedTime = "Akhir Pekan"
  
      lateinit var eventAdapter : GeneralEventRecyclerViewAdapter
      private var  eventList = ArrayList&lt;Event&gt;()
  
      override fun onAttach(context: Context) {
          super.onAttach(context)
  
          mContext = context
          activity?.let { mActivity = it }
  
      }
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          fragmentView = inflater.inflate(R.layout.fragment_search_result, container, false)
  
          setUpSafeArg()
          setUpViewsDeclaration()
          setUpListeners()
  
          initRecyclerView()
  
  
          return fragmentView
      }
  
      private fun setUpViewsDeclaration() {
          recyclerView = fragmentView.findViewById(R.id.recyclerView_event_general_search_result)
          tabLayout = fragmentView.findViewById(R.id.tabLayout_time_search_result)
  
      }
  
      private fun setUpSafeArg() {
          arguments?.let {
  
              val args = SearchResultFragmentArgs.fromBundle(it)
              selectedEventType = args.selectedEventType
              selectedCity = args.selectedCity
              selectedTime = args.selectedTime
  
          }
      }
  
      private fun setUpListeners() {
  
          tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
  
              override fun onTabSelected(tab: TabLayout.Tab?) {
  
                  when(tab?.position ?: 0) {
                      0 -&gt; mActivity.toast("0")
                      1 -&gt;  mActivity.toast("1")
                      2 -&gt;  mActivity.toast("2")
                      3 -&gt;  mActivity.toast("3")
                      4 -&gt;  mActivity.toast("0")
                      else -&gt; println("Number too high")
                  }
  
              }
  
              override fun onTabReselected(tab: TabLayout.Tab?) {
  
              }
  
              override fun onTabUnselected(tab: TabLayout.Tab?) {
  
              }
  
  
          })
  
      }
  
  
  
  
      private fun initRecyclerView() {
  
          eventAdapter = GeneralEventRecyclerViewAdapter(mContext)
          val layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL,false)
          recyclerView.adapter = eventAdapter
          recyclerView.layoutManager = layoutManager
  
          eventAdapter.setOnItemClickListener(object: OnEventKMListener {
  
              override fun eventKMClicked(position: Int) {
                  val selectedEvent = eventList[position]
                  val eventDetailDestination = SearchResultFragmentDirections.actionGlobalDestinationEventDetail(selectedEvent)
                  Navigation.findNavController(fragmentView).navigate(eventDetailDestination)
              }
  
          })
  
      }
  
  }
  </code></pre>
  
  <p>but I don't know why I can't change the tab when I run the app, I also can't swipe that tab. the tab is stay on index 0</p>
  
  <p>here is my xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout 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"
                                                     tools:context=".Fragments.Search.SearchResultFragment" android:id="@+id/constraintLayout_search_result"&gt;
  
  
      &lt;com.google.android.material.tabs.TabLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
              app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp" android:layout_marginTop="16dp"
              app:layout_constraintTop_toTopOf="parent" android:id="@+id/tabLayout_time_search_result"&gt;
  
          &lt;com.google.android.material.tabs.TabItem
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Hari Ini"/&gt;
  
          &lt;com.google.android.material.tabs.TabItem
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Besok"/&gt;
  
          &lt;com.google.android.material.tabs.TabItem
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Akhir Pekan"/&gt;
  
          &lt;com.google.android.material.tabs.TabItem
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Pekan Depan"/&gt;
  
          &lt;com.google.android.material.tabs.TabItem
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Semua"/&gt;
  
      &lt;/com.google.android.material.tabs.TabLayout&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
              android:layout_width="379dp"
              android:layout_height="635dp"
              app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
              app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
              app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="16dp"
              app:layout_constraintTop_toBottomOf="@+id/tabLayout_time_search_result"
              android:id="@+id/recyclerView_event_general_search_result"
              tools:listitem="@layout/item_general_event"/&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>and here is my MainActivity:</p>
  
  <pre><code>class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener {
  
      private lateinit var navController : NavController
      lateinit var destinationTitleTextView : TextView
      lateinit var progressBar : ProgressBar
      lateinit var topToolbar : Toolbar
      lateinit var bottomNavigationView : BottomNavigationView
  
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          FirebaseApp.initializeApp(this)
  
          // Initial Setup views
          navController = Navigation.findNavController(this,R.id.nav_host_fragment)
          setupBottomNavMenu(navController)
          setupActionBar(navController)
          setUpViewDeclaration()
  
  
          // Add Listeners
          navController.addOnDestinationChangedListener(this)
  
  
      }
  
  
  
      override fun dispatchTouchEvent(event: MotionEvent): Boolean {
  
          // to hide softkeyboard when tapping outside area in the fragment
          // And in the layout of the fragment use this attribute: android:focusableInTouchMode="true"
  
          if (event.action == MotionEvent.ACTION_DOWN) {
              val v = currentFocus
              if (v is EditText) {
                  val outRect = Rect()
                  v.getGlobalVisibleRect(outRect)
                  if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
                      v.clearFocus()
                      val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                      imm.hideSoftInputFromWindow(v.windowToken, 0)
                  }
              }
          }
          return super.dispatchTouchEvent(event)
      }
  
  
  
      private fun setUpViewDeclaration() {
          destinationTitleTextView = findViewById(R.id.destination_label_text_view)
          progressBar = findViewById(R.id.progressBar_main_activity)
          topToolbar = findViewById(R.id.top_toolbar)
          bottomNavigationView = findViewById(R.id.bottom_nav)
  
      }
  
      private fun setupBottomNavMenu(navController: NavController) {
          bottom_nav.setupWithNavController(navController)
      }
  
  
      private fun setupActionBar(navController: NavController) {
  
          setSupportActionBar(top_toolbar)
          supportActionBar?.setDisplayShowTitleEnabled(false)
  
          // set up top hierarchy destination
          val appBarConfiguration = AppBarConfiguration(setOf(
              R.id.destination_home,
              R.id.destination_search,
              R.id.destination_user_control,
              R.id.destination_create_event)
          )
  
          top_toolbar.setupWithNavController(navController,appBarConfiguration)
  
      }
  
      override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
  
          progressBar.visibility = View.GONE // to ensure progress bar will be gone whenever the user segues to other destination
  
  
  
  
          // set toolbar and bottom navigation visibility
          if (destination.id == R.id.destination_welcome ||
              destination.id == R.id.destination_authentication ||
              destination.id == R.id.destination_loginUsingEmail ||
              destination.id == R.id.destination_registerUsingEmail ||
              destination.id == R.id.destination_choose_user_location ||
              destination.id == R.id.destination_change_password ||
              destination.id == R.id.destination_create_event_finished_info ||
              destination.id == R.id.destination_photo_view
  
          ) {
  
              topToolbar.visibility = View.GONE
              bottomNavigationView.visibility = View.GONE
  
          } else if (
  
              destination.id == R.id.destination_create_event_name ||
              destination.id == R.id.destination_create_event_type ||
              destination.id == R.id.destination_create_event_starting_date ||
              destination.id == R.id.destination_create_event_ending_date ||
              destination.id == R.id.destination_create_event_speaker ||
              destination.id == R.id.destination_create_event_description ||
              destination.id == R.id.destination_create_event_poster ||
              destination.id == R.id.destination_create_event_coordinate ||
              destination.id == R.id.destination_create_event_location_detail ||
              destination.id == R.id.destination_create_event_contact ||
              destination.id == R.id.destination_create_event_price ||
              destination.id == R.id.destination_create_event_capacity ||
              destination.id == R.id.destination_create_event_summary ||
              destination.id == R.id.destination_create_event_finished_info ||
              destination.id == R.id.destination_event_detail ||
              destination.id == R.id.destination_event_description_detail ||
              destination.id == R.id.destination_map_detail_location
  
          ) {
  
              topToolbar.visibility = View.VISIBLE
              bottomNavigationView.visibility = View.GONE
  
          } else if (
              destination.id == R.id.destination_user_control ||
              destination.id == R.id.destination_search
  
          ) {
  
              topToolbar.visibility = View.GONE
              bottomNavigationView.visibility = View.VISIBLE
  
          } else {
  
              topToolbar.visibility = View.VISIBLE
              bottomNavigationView.visibility = View.VISIBLE
  
          }
  
  
  
  
  
  
  
          // Set Toolbar Title
          if (destination.id == R.id.destination_home) {
              destinationTitleTextView.setText(R.string.home)
          } else if (destination.id == R.id.destination_create_event_summary) {
              destinationTitleTextView.text = "Ringkasan"
          } else if (
              destination.id == R.id.destination_create_event ||
              destination.id == R.id.destination_create_event_name ||
              destination.id == R.id.destination_create_event_type ||
              destination.id == R.id.destination_create_event_starting_date ||
              destination.id == R.id.destination_create_event_ending_date ||
              destination.id == R.id.destination_create_event_speaker ||
              destination.id == R.id.destination_create_event_description ||
              destination.id == R.id.destination_create_event_poster ||
              destination.id == R.id.destination_create_event_coordinate ||
              destination.id == R.id.destination_create_event_location_detail ||
              destination.id == R.id.destination_create_event_contact ||
              destination.id == R.id.destination_create_event_capacity ||
              destination.id == R.id.destination_create_event_price ||
              destination.id == R.id.destination_create_event_summary ||
              destination.id == R.id.destination_create_event_finished_info
  
          ) {
  
              destinationTitleTextView.setText(R.string.createEvent)
  
          } else {
  
              destinationTitleTextView.text = ""
  
          }
  
  
  
  
      }
  
  
  }
  </code></pre>
  
  <p>what went wrong in here ?
  java is ok</p>